How can i get access token google developer using OAuth2.0 C#

Clash Royale CLAN TAG#URR8PPP
How can i get access token google developer using OAuth2.0 C#
I want to access token to call another api of google for i which i need token. I am getting the following error in JSON.
"error" : "invalid_request",
"error_description" : "Missing required parameter: scope"
Here is my code to get access token.
var client = new RestClient("https://accounts.google.com/o/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret + "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;
I just want to get access token.
1 Answer
1
At least try to pass scope parameter as error message suggests:
var scope_string = "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email";
request.AddParameter("application/x-www-form-urlencoded", $"grant_type=client_credentials&client_id=client_id&client_secret=client_secret&scope=scope_string", ParameterType.RequestBody);
In case you receive forbidden. You need to enable Google+ API in developer console. Follow their manual (Acquiring and using an API key):
https://developers.google.com/+/web/api/rest/oauth
Before you ask for token you need to authorize using another rest service (https://accounts.google.com/o/oauth2/auth) with "&response_type=code" parameter and then use authorization code as a parameter in the token request to get token:
...?code=Authentication Code from auth&...
googleapis.com/auth/userinfo.profile%20https://…
– Shamshad Jamal
46 mins ago
@ShamshadJamal see updated answer
– Access Denied
37 mins ago
Google+Api is already enabled
– Shamshad Jamal
30 mins ago
Have you created Client ID and client secret in google developer console and used it in your code?
– Access Denied
25 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I have try it but it gives StatusCode: Forbidden
– Shamshad Jamal
48 mins ago