Я пытаюсь получить токен доступа с помощью почтового запроса на https://oauth2.googleapis.com/token, но он продолжает выдавать ошибку:
"Произошла ошибка при отправке запроса".
Он отлично работает через Postman и Fiddler с одинаковым содержимым. Подскажите пожалуйста, что я делаю не так в коде?
Код: Выделить всё
Uri myUri = new Uri(request.AbsoluteUrl);
string googleCode = HttpUtility.ParseQueryString(myUri.Query).Get("code");
var googleClientId = "435335443.apps.googleusercontent.com";
var uri = "https://oauth2.googleapis.com/token";
var googleClientSecret = "Test123";
var _httpClient = new HttpClient();
var data = new GoogleAccessToken
{
clientId = googleClientId,
clientSecret = googleClientSecret,
grant_type = "authorization_code",
redirect_uri = "http://localhost:53419/GoogleOAuth", //Same as one I used to get the code
code = googleCode
};
var jsonRequest = JsonConvert.SerializeObject(data);
var rawResponse = await _httpClient.PostAsync(uri, new StringContent(jsonRequest, Encoding.UTF8, "application/json"));
Json request
Код: Выделить всё
{"code":"657856987608768-asfdsacsdcsd","client_Id":"435335443.apps.googleusercontent.com","client_secret":"Test123","redirect_uri":"http://localhost:53419/GoogleOAuth","grant_type":"authorization_code"}
Код: Выделить всё
POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/json
{
"code":"657856987608768-asfdsacsdcsd",
"client_id":"435335443.apps.googleusercontent.com",
"client_secret":"Test123",
"redirect_uri":"http://localhost:53419/GoogleOAuth",
"grant_type":"authorization_code"
}
Источник: https://stackoverflow.com/questions/610 ... code-works