I am facing issue for retrieving access token from refresh token

74 views
Skip to first unread message

Hardik Masalawala

unread,
Oct 30, 2022, 6:48:17 AM10/30/22
to Google Ads API and AdWords API Forum
I am not able to get new access token using refresh token 
ERROR : The remote server returned an error: (400) Bad Request.

public static string PostMethod(string baseAddress, string data)
{
    try
    {
        HttpWebRequest request = HttpWebRequest.Create(baseAddress) as HttpWebRequest;
        string result = null;
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded;";
        string param = string.Format(data);
        var bs = Encoding.UTF8.GetBytes(param);
        using (Stream reqStream = request.GetRequestStream())
        {
            reqStream.Write(bs, 0, bs.Length);
        }
        using (WebResponse response = request.GetResponse())
        {
            var sr = new StreamReader(response.GetResponseStream());
            result = sr.ReadToEnd();
            sr.Close();
        }
        return result;
    }
    catch (Exception ex)
    {
        throw;
    }
}


private string GetToken(GmailIntegrationModel gmailIntegrationModel, string subscribedCompanyId)
{
    _logger.LogDebug("GetToken in.");
    try
    {
        //string Url = "https://www.googleapis.com/oauth2/v4/token?";
        string Url = "https://accounts.google.com/o/oauth2/token";
        var credentials = GetGmailCredentials(subscribedCompanyId);
        var data = "client_id=" + credentials.UserName + "&client_secret=" + credentials.ApiKey + "&refresh_token=" + gmailIntegrationModel.Refresh_Token + "&grant_type=refresh_token";
        var result = CommonMethod.PostMethod(Url, data);
        var res = JsonConvert.DeserializeObject<GmailIntegrationModel>(result);
        return res.Access_token;

    }
    catch (Exception ex)
    {
        _logger.LogError(ex.Message, ex);
        throw;
    }
}

Google Ads API Forum Advisor

unread,
Oct 31, 2022, 3:08:50 AM10/31/22
to hardikmas...@gmail.com, adwor...@googlegroups.com

Hi Hardik,
 

Thanks for reaching out to the Google Ads API team. I hope you are doing well today.
 

With regards to your concern, could you please confirm if you are using Google Ads API? If yes, you can use our client library and for you to generate a refresh token you may refer to this guide.
 

Furthermore, If you are using the rest api, you may refer to this documentation.


Regards,

Google Logo
Darwin
Google Ads API Team
 


ref:_00D1U1174p._5004Q2fpmJN:ref
Reply all
Reply to author
Forward
0 new messages