如何在.NET中使用刷新标记通过谷歌驱动器SDK来生成访问令牌? [英] How to generate access token using refresh token through Google Drive SDK in .NET?
问题描述
我有一个使用谷歌驱动器来访问用户的文件.NET应用程序。我能够获得授权,code,我已经能够通过AccessToken和RefreshToken交换授权code。问题是,我不能刷新访问令牌,并在一个小时后到期。
与此类似的问题:<一href="http://stackoverflow.com/questions/10631042/how-to-generate-access-token-using-refresh-token-through-google-drive-api">How通过谷歌驱动的API使用刷新标记生成访问令牌?的不同之处在于,我在.NET中工作(使用Google.APIs ...... DLL文件)。
我很清楚这一点:<一href="https://developers.google.com/accounts/docs/OAuth2InstalledApp#refresh">https://developers.google.com/accounts/docs/OAuth2InstalledApp#refresh但是,我期待某种形式的IAuthorizationState或OAuth2Authenticator对象提供的方法,让我刷新访问令牌。
请指教。谢谢。
请注意,使用这个code,我能够获得访问令牌。这只是我期待这个code是谷歌API里面。
公共类OAuth2AccessTokenReponse
{
公共字符串access_token;
公众诠释expires_in;
公共字符串token_type;
}
公共静态字符串refreshAccessToken()
{
使用(System.Net.WebClient客户端=新System.Net.WebClient())
{
byte []的响应= client.UploadValues(https://accounts.google.com/o/oauth2/token,新System.Collections.Specialized.NameValueCollection(){
{CLIENT_ID,客户端ID},
{client_secret,ClientSecret},
{refresh_token,XXXXX},
{grant_type,refresh_token}
});
串sresponse = System.Text.Encoding.Default.GetString(响应);
OAuth2AccessTokenReponse O =(OAuth2AccessTokenReponse)Newtonsoft.Json.JsonConvert.DeserializeObject(sresponse的typeof(OAuth2AccessTokenReponse));
返回o.access_token;
}
}
我学的是一个更合适的样品:在GoogleApisSample的Tasks.WinForms.NoteMgr ......并用它,我找到了解决办法。
解决的方法是在低于code。它的关键部分是调用的 arg.RefreshToken(状态); 的
感谢。
公共静态Authentication.IAuthenticator UseSavedAuthorization()
{
VAR提供商=新NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier =客户端ID;
provider.ClientSecret = ClientSecret;
OAuth2Authenticator&LT; NativeApplicationClient&GT; AUTH =新OAuth2Authenticator&LT; NativeApplicationClient&GT;(供应商的getState);
auth.LoadAccessToken();
返回身份验证;
}
公共静态IAuthorizationState的getState(NativeApplicationClient ARG)
{
IAuthorizationState状态=新AuthorizationState(新[] {TasksService.Scopes.Tasks.GetStringValue()
DriveService.Scopes.DriveFile.GetStringValue(),DriveService.Scopes.Drive.GetStringValue()
});
state.Callback =新的URI(NativeApplicationClient.OutOfBandCallbackUrl);
state.RefreshToken =&LT;刷新令牌previously保存&gt;中;
arg.RefreshToken(州);
返回的状态;
}`
I have a .NET application that is using Google Drive to access the user's file. I am able to get the authorization code, and I have been able to exchange the authorization code by the AccessToken and the RefreshToken. The issue is that I cannot refresh the access token, and it expires after an hour.
Similar to this question: How to generate access token using refresh token through google drive API? except that I am working in .NET (using the Google.APIs... DLLs).
I am aware of this: https://developers.google.com/accounts/docs/OAuth2InstalledApp#refresh however, I am expecting some sort of method available in the IAuthorizationState or OAuth2Authenticator object to allow me refresh the access token.
Please advise. Thanks.
Please note that using this code I am able to get the Access Token. It is just that I am expecting this code to be inside the Google API.
public class OAuth2AccessTokenReponse
{
public string access_token;
public int expires_in;
public string token_type;
}
public static string refreshAccessToken()
{
using (System.Net.WebClient client = new System.Net.WebClient())
{
byte[] response = client.UploadValues("https://accounts.google.com/o/oauth2/token", new System.Collections.Specialized.NameValueCollection(){
{"client_id", ClientID},
{"client_secret", ClientSecret},
{"refresh_token", "XXXXX"},
{"grant_type", "refresh_token"}
});
string sresponse = System.Text.Encoding.Default.GetString(response);
OAuth2AccessTokenReponse o = (OAuth2AccessTokenReponse) Newtonsoft.Json.JsonConvert.DeserializeObject(sresponse, typeof(OAuth2AccessTokenReponse));
return o.access_token;
}
}
I studied a more suitable sample: the Tasks.WinForms.NoteMgr of the GoogleApisSample... and with it I found the solution.
The solution is in the code below. The key part of it is calling arg.RefreshToken(state);
Thanks.
public static Authentication.IAuthenticator UseSavedAuthorization()
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = ClientID;
provider.ClientSecret = ClientSecret;
OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(provider, getState);
auth.LoadAccessToken();
return auth;
}
public static IAuthorizationState getState(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue(),
DriveService.Scopes.DriveFile.GetStringValue() , DriveService.Scopes.Drive.GetStringValue()
});
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
state.RefreshToken = "<refresh token previously saved>";
arg.RefreshToken(state);
return state;
}`
这篇关于如何在.NET中使用刷新标记通过谷歌驱动器SDK来生成访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!