問題描述
我一直在查看 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 使用刷新令牌進行身份驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!