2013-12-11 50 views
0

在我的c#桌面應用中,我使用Google的API對API的訪問令牌進行身份驗證和檢索。我注意到API會緩存這個令牌並且再次使用它,直到它到期。刪除已撤銷的緩存的Google訪問令牌

使用我的瀏覽器,我可以撤銷使用令牌:我這樣做是爲了測試出API如何處理撤銷令牌

https://accounts.google.com/o/oauth2/revoke?token=1/xxxxxxx 

。按照規定,API失敗。我遇到的問題是讓API使用新的令牌。它繼續檢索緩存的令牌,即使它已被撤銷。下面的代碼是用來使用的API認證:

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None); 

?我怎樣才能刪除緩存的令牌和API申請一個新的?

回答

1

您必須在獲取新令牌之前註銷。您可以在這裏閱讀OAuth 2.0 https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth?hl=uk。 下面怎麼我已經做了我的Windows Phone 8應用:

VAR字符串常量GoogleTokenFileName = 「Google.Apis.Auth.OAuth2.Responses.TokenResponse用戶」

公共異步任務LoginAsync()

{ 
      await Logout(); 
      _credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets 
      { 
       ClientId = _xmlfile.GoogleClientId, 
       ClientSecret = _xmlfile.GoogleClientSecret 
      }, new[] { Oauth2Service.Scope.UserinfoProfile }, "user", CancellationToken.None); 

      return session; 
    } 

公共異步任務退出()

{ 
     await new WebBrowser().ClearCookiesAsync(); 
     if (_storageService.FileExists(GoogleTokenFileName)) 
     { 
      _storageService.DeleteFile(GoogleTokenFileName); 
     } 
    } 

希望它能幫助。