问题描述
我想知道如何使用Google Api的访问令牌来获取用户详细信息,名字,姓氏和电子邮件ID。目前,我正在使用下面的代码返回我
{ "error": "invalid_grant","error_description": "Bad Request" },
使用RestSharp的服务器端c#代码
var client = new RestClient("https://oauth2.googleapis.com/token");
var request = new RestRequest(Method.POST);
request.AddParameter("code",accesstoken);
request.AddParameter("redirect_uri","[my domain ]/oauth?provider=google");
request.AddParameter("client_id","[ClientId]");
request.AddParameter("client_secret","[ClientSecret]");
request.AddParameter("scope","openid email profile");
request.AddParameter("grant_type","authorization_code");
IRestResponse response = client.Execute(request);
在Google控制台的OAuth consent screen
中,我没有在Scopes
下添加任何内容。这可能是个问题吗?我仅访问基本信息。另外,我不确定要添加哪些范围。
在此先感谢并感谢您的帮助。
更新:我尝试添加此范围-https://www.googleapis.com/auth/userinfo.profile,但仍然存在相同的问题。
https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=<Redirect url>?provider=google&prompt=consent&response_type=code&client_id=<Client id>&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&access_type=offline
点击此链接后,用户将被重定向到Google身份验证页面。一旦通过身份验证,它将使用访问令牌重定向到重定向URL,然后在服务器端进行验证并获取用户信息。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)