使用Google API在服务器端获取用户信息

问题描述

我想知道如何使用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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...