Help me please. I have tried get a user information from googleApis. But I get error already. My error: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth2accesstoken, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status": "UNAUTHENTICATED" } }
I use this method
var userInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo";
var hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
var response = hc.GetAsync(userInfoUrl).Result;
var userInfo = response.Content.ReadAsStringAsync().Result;
This I get access token:
string codeClient = "code=" + code + $"&client_id={googleclientid}&";
string secretUri = $"client_secret={googleusersecretkey}&" + $"redirect_uri={googleredirect}&"
+ "grant_type=authorization_code";
string postString = codeClient + secretUri;
string url = "https://accounts.google.com/o/oauth2/token";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
UTF8Encoding utfenc = new UTF8Encoding();
byte[] bytes = utfenc.GetBytes(postString);
Stream os = null;
request.ContentLength = bytes.Length;
os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseStreamReader = new StreamReader(responseStream);
var result = responseStreamReader.ReadToEnd();
var parametrs = result.Split(',');
return parametrs[0].Replace("\"", "").Remove(0, 18);
when I do request from https://www.googleapis.com/oauth2/v1/tokeninfo Then I get response
{
"issued_to": "MyClientId",
"audience": "MyClientId",
"scope": "googleapis.com/auth/drive",
"expires_in": 3549,
"access_type": "offline"
}
This token is OK