问题描述
我一直在查看 Oauth .Net Google API,以便通过 OAuth 进行身份验证并使用 Google drive API.
I've been looking at the Oauth .Net Google Apis in order to authenticate via OAuth and use the Google drive Apis.
具体来说,我想使用我已经存储的刷新令牌来实例化 GoogleDrive 服务.
Specifically, I want to use a refresh token I already have stored in order to use it to instantiate a GoogleDrive service.
我找到了类似的样本https:///code.google.com/p/google-api-dotnet-client/source/browse/Tasks.SimpleOAuth2/Program.cs?repo=samples
这似乎使用GoogleWebAuthorizationBroker.AuthorizeAsync",但我不确定如何使用刷新令牌而不是您在本示例中提供的客户端机密来使用该方法.
That seem to use "GoogleWebAuthorizationBroker.AuthorizeAsync" but I'm not sure how I can use that method with a refresh token rather than the client secrets you seem to be feeding it in this example.
推荐答案
如果我理解正确,您是在问如何根据现有的刷新令牌创建新的 Google 服务.
If I understand you correctly, you are asking how can you create a new Google service, based on an existing refresh token.
因此,您可以执行以下操作:
So, you can do the following:
var token = new TokenResponse { RefreshToken = "YOUR_REFRESH_TOKEN_HERE" };
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = [your_client_secrets_here]
}), "user", token);
然后您可以将您的凭据传递给服务的初始化程序.
Then you can pass your credentials to the service's initializer.
通过执行上述操作,GoogleAuthorizationCodeFlow 将根据您刷新令牌和客户端机密获取新的访问令牌.
By doing the above, GoogleAuthorizationCodeFlow will get a new access token based on you refresh token and client secrets.
请注意,您必须在此处拥有客户端密码,否则您将无法获得访问令牌.
Note that you must have client secrets here, without that, you won't be able to get an access token.
这篇关于.NET Google api 1.7 beta 使用刷新令牌进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!