Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何实现GoogleAPI刷新令牌_C#_Oauth 2.0_Oauth_Google Api_Google Oauth - Fatal编程技术网

C# 如何实现GoogleAPI刷新令牌

C# 如何实现GoogleAPI刷新令牌,c#,oauth-2.0,oauth,google-api,google-oauth,C#,Oauth 2.0,Oauth,Google Api,Google Oauth,我一直在做一个项目,我不得不在7个不同的平台上实现OAuth,但由于某种原因,Google的OAuth进程让我绊倒了 该项目是在.NETMVC中开发的,我正在使用RestSharp发出Http请求 我正在检索一个OAuth请求以访问GoogleAnalytics API,在第一次尝试时,我获得了一个有效的访问令牌,我能够使用该令牌成功地从用户的GA帐户检索数据。但是,尽管我在身份验证请求URL中指定了access_type=offline,但在令牌过期后尝试刷新令牌时,我看到以下错误: 400-

我一直在做一个项目,我不得不在7个不同的平台上实现OAuth,但由于某种原因,Google的OAuth进程让我绊倒了

该项目是在.NETMVC中开发的,我正在使用RestSharp发出Http请求

我正在检索一个OAuth请求以访问GoogleAnalytics API,在第一次尝试时,我获得了一个有效的访问令牌,我能够使用该令牌成功地从用户的GA帐户检索数据。但是,尽管我在身份验证请求URL中指定了access_type=offline,但在令牌过期后尝试刷新令牌时,我看到以下错误:

400-错误请求-{ “错误”:“无效的授权”, “错误描述”:“错误请求” }

我的请求URL如下所示:

https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https://www.googleapis.com/auth/analytics.readonly&include_granted_scopes=true&response_type=code&state=8f692f0f-b177-4b0b-aa89-a757da9432e3和重定向=https://localhost:44338/GoogleAnalytics&client_id=xxxx

当用户被重定向回代码时调用My token request函数:

public OAuth2Token GetToken(string code)
{
    var client = new RestClient(_tokenUri);
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("undefined", $"client_id={_clientId}&client_secret={_secret}&code={code}&redirect_uri={_redirectUri}&grant_type=authorization_code", ParameterType.RequestBody);
    var response = client.Execute<OAuth2Token>(request);
    return response.Data;
}
public OAuth2Token GetToken(字符串代码)
{
var client=新的RestClient(_tokenUri);
var请求=新的重新请求(Method.POST);
request.AddHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
AddParameter(“undefined”,$“client\u id={\u clientId}和client\u secret={\u secret}&code={code}&redirect\u uri={\u redirectUri}&grant\u type=authorization\u code”,ParameterType.RequestBody);
var response=client.Execute(请求);
返回响应数据;
}
返回Access&refresh令牌,并将其存储在数据库中,然后在“我的刷新”函数中稍后使用,其中令牌URI为:

public OAuth2Token RefreshToken()
{
var client=新的RestClient(_tokenUri);
var请求=新的重新请求(Method.POST);
request.AddHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
AddParameter(“未定义“,$”客户端id={{U客户端id}和客户端密码={U密码}和刷新令牌={U刷新令牌}和授予类型=刷新令牌”,ParameterType.RequestBody);
var response=client.Execute(请求);
如果(!response.issucessful)
if(response.ErrorException==null)
抛出新异常(response.Content);
其他的
抛出response.ErrorException;
返回响应数据;
}
另一点需要注意的是,我的谷歌应用程序处于测试模式,我已经将用户添加到测试用户列表中。关于我做错了什么有什么建议吗


Cheers

post请求希望看到post正文中的值。您正在将它们作为参数发送

  request.AddParameter("undefined", $"client_id={_clientId}&client_secret={_secret}&refresh_token={_refreshToken}&grant_type=refresh_token", ParameterType.RequestBody);
您可能应该使用request.AddBody

 request.AddBody("undefined", $"client_id={_clientId}&client_secret={_secret}&refresh_token={_refreshToken}&grant_type=refresh_token", ParameterType.RequestBody);
详细解释。 让我们从头开始

谷歌授权或者实际上所有的Oauth2服务器都是所谓的Oauth舞蹈,这个舞蹈有三个步骤

请求同意 您需要的第一个URL将导致授权服务器向用户显示同意屏幕

GET https://accounts.google.com/o/oauth2/auth?client_id={clientid}&redirect_uri={redirectURI}&scope={scope}&response_type=code
这将返回给您一个授权代码注意响应类型是response\u type=code

交换访问令牌和刷新令牌的授权代码。 这个调用是一个HTTP Post,主体是HTTP请求字符串的形式

POST https://accounts.google.com/o/oauth2/token
code={AUTHORIZATION CODE}&client_id={ClientId}&client_secret={ClientSecret}&redirect_uri={REDIRECT URI}&grant_type=authorization_code
这里的响应将是Json格式的,并且将是刷新令牌和访问令牌。请注意,此处的授权类型是授权代码

{
"access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4"
}
由于您的访问令牌将在一小时后过期,您需要使用刷新令牌刷新它

POST https://accounts.google.com/o/oauth2/token
client_id={CLIENT ID}&client_secret={ClientSecret}&refresh_token={REFRESH TOKEN}&grant_type=refresh_token
刷新访问令牌。 这也是一个HTTP POST调用,正文也是以查询字符串的形式出现的。 注意,这里的授权类型是刷新令牌

POST https://accounts.google.com/o/oauth2/token
client_id={CLIENT ID}&client_secret={ClientSecret}&refresh_token={REFRESH TOKEN}&grant_type=refresh_token
然后您有一个新的访问令牌

{
"access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
"token_type" : "Bearer",
"expires_in" : 3600
}

您不使用google.net客户端库的原因是什么?@DaImTo考虑到整个项目中的每个API都使用相同的OAuth2行业标准,但没有一个API共享相同的库,因此决定代码可读性和一致性。理论上,只要他们遵循标准,这就不重要了。