如何通过.NET中的Google Drive SDK使用刷新令牌生成访问令牌?

如何通过.NET中的Google Drive SDK使用刷新令牌生成访问令牌?,.net,google-drive-api,.net,Google Drive Api,我有一个.NET应用程序,它使用Google Drive访问用户的文件。我能够获得授权代码,并且我能够通过AccessToken和RefreshToken交换授权代码。问题是我无法刷新访问令牌,并且它在一小时后过期 类似于这个问题:除了我在.NET中工作(使用Google.api…DLL) 我知道这一点:但是,我希望IAuthorizationState或OAuth2Authenticator对象中提供某种方法,允许我刷新访问令牌 请告知。谢谢 请注意,使用此代码我可以获得访问令牌。我只是希望这

我有一个.NET应用程序,它使用Google Drive访问用户的文件。我能够获得授权代码,并且我能够通过AccessToken和RefreshToken交换授权代码。问题是我无法刷新访问令牌,并且它在一小时后过期

类似于这个问题:除了我在.NET中工作(使用Google.api…DLL)

我知道这一点:但是,我希望IAuthorizationState或OAuth2Authenticator对象中提供某种方法,允许我刷新访问令牌

请告知。谢谢

请注意,使用此代码我可以获得访问令牌。我只是希望这段代码在GoogleAPI中

    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;        
        }
    }

如果您正在使用.NET客户端库(http://code.google.com/p/google-api-dotnet-client/),你不必管它。库将在需要时使用您第一次检索到的刷新令牌自动为您请求新的访问令牌。

我研究了一个更合适的示例:GoogleAppSample的Tasks.WinForms.NoteMgr。。。有了它,我找到了解决办法

解决方案在下面的代码中。它的关键部分是调用arg.refreshttoken(state)

谢谢

    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; 
    }`
public static Authentication.iaauthenticator UseSavedAuthorization()
{          
var provider=new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier=ClientID;
provider.ClientSecret=ClientSecret;
OAuth2Authenticator auth=新的OAuth2Authenticator(提供者,getState);
auth.LoadAccessToken();
返回auth;
}
公共静态IAAuthorizationState getState(NativeApplicationClient参数)
{
IAAuthorizationState=新授权状态(新[]{TasksService.Scopes.Tasks.GetStringValue(),
DriveService.Scopes.DriveFile.GetStringValue(),DriveService.Scopes.Drive.GetStringValue()
});
state.Callback=新Uri(NativeApplicationClient.OutOfBandCallbackUrl);
state.RefreshToken=“”;
参数刷新令牌(状态);
返回状态;
}`

我正在使用库。我的项目是一个“已安装的应用程序”。在我的测试中,它不起作用。我能够在第一时间访问应用程序(在此期间打开/关闭我的应用程序有效)。一小时后,应用程序因此问题被拒绝访问。你有这样做的工作样本吗?我使用的是Task.SimpleOAuth2示例(当然可以扩展它)。用户第一次授予你的应用访问权限时,你只能获得一个刷新令牌,因此你必须存储它以备将来使用。我猜您第二次尝试该应用时,只检索了一个访问令牌(并且没有存储刷新令牌),因此无法在其过期后请求新的访问令牌。Google Drive有一个完整的示例,但它不是一个已安装的应用程序:Claudio,是的-我第一次得到了刷新令牌并保存了它。下一次我尝试使用它时(通过设置state.refreshttoken={my refreshttoken},并使用authorization.LoadAccessToken()),这不起作用。注意:我还查看了DrEdit.Net示例,在我看来他们正在做我正在做的事情。请注意,我上面的解决方案代码起作用(因此我有正确的refreshttoken)。谢谢你的帮助。顺便说一句:我可以接受。只是按照你说的那样自动刷新会更方便。而且,如果我做了一些根本错误的事情,这也会让我感到害怕。我认为谷歌的示例很好,但它们有太多的依赖性(就像他们有内部类来保存ClientID和ClientSecret等)。我简化了任务。SimpleAuth2尽可能删除其他依赖项(即:保留进行身份验证的“核心”)。除了这个细节,一切都正常。克劳迪奥,我找到了解决方案-请参阅我的答案。谢谢你的帮助。