0

I want to get access token from refresh token to send email in the server But I get 400,"Bad Request" error. here is the code I use on client side,

      var CLIENT_ID = 'A';
      var CLIENT_SECRET = 'B';

      var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"];
      var SCOPES = 'https://mail.google.com/';

function initClient() {
    gapi.client.init({
        clientId: CLIENT_ID,
        discoveryDocs: DISCOVERY_DOCS,
        scope: SCOPES
    }).then(function() {
        gapi.auth2.getAuthInstance().grantOfflineAccess().then(offline);
    }, function(error) {

    });
}

function offline(rtn) {
    var refreshToken = rtn.code;

}

and here is the c# .net code I use on the server side to get access token

var secrets = new ClientSecrets
            {
                ClientId = "A",
                ClientSecret = "B"
            };


            HttpClient xclient = new HttpClient();
            xclient.BaseAddress = new Uri("https://www.googleapis.com");

            var refreshMessage = new HttpRequestMessage(HttpMethod.Post, "/oauth2/v4/token")
            {
                Content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]
            {
                new KeyValuePair<string, string>("client_id", secrets.ClientId),
                new KeyValuePair<string, string>("client_secret", secrets.ClientSecret),
                new KeyValuePair<string, string>("refresh_token", refreshToken),
                new KeyValuePair<string, string>("grant_type", "refresh_token")
            })
            };


            Task.Run(async () =>
            {
                var response = await xclient.SendAsync(refreshMessage);
                if (response.IsSuccessStatusCode)
                {
                    var tokenResponse = await response.Content.ReadAsStringAsync();


                }
            }).GetAwaiter().GetResult();

What could be wrong?

user818117
  • 420
  • 1
  • 5
  • 15
  • Possible duplicate of [Using refresh\_token for Google OAuth 2.0 returns http 400 bad request](https://stackoverflow.com/questions/18820302/using-refresh-token-for-google-oauth-2-0-returns-http-400-bad-request) – Greg May 05 '19 at 22:04
  • @Greg I tied that with httpie and also from .net with Content-Type: "application/x-www-form-urlencoded; charset=utf-8" just didnt work , note that that is 2013 thread ,google now uses different url ```/oauth2/v4/token``` – user818117 May 06 '19 at 06:19

0 Answers0