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?

0 Answers