2016-04-30 18 views
1

Xamarin.Authを使用してユーザーを認証し、GoogleカレンダーAPIにアクセスしています。私はアクセストークンを取得し、必要なものを実行することができます。問題はaccess_tokenです.1時間後に有効期限が切れます。私はいくつかの記事でこれに関する例と情報を探しましたが、私はまだ新しいトークンを要求することはできません。ここで私は私のXamarin.Droidプロジェクトで使用しているコードです:下図のようにGoogle OAuth 2 - Xamarin.Authを使用して更新トークンを取得する方法

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     global::Xamarin.Forms.Forms.Init(this, bundle); 
     LoadApplication(new App()); 

     #region [GoogleAuthentication] 

     if (App.auth == null) 
     { 
      App.auth = new GoogleAuthenticator(App.WebApplicationClientID, 
      new Uri("https://www.googleapis.com/plus/v1/people/me"), 
      CalendarService.Scope.Calendar); 
     } 

     // We don't want to have to login every time, so we'll use the Xamarin.Auth AccountStore 
     AccountStore store = AccountStore.Create(this); 
     Account savedAccount = store.FindAccountsForService("google").FirstOrDefault(); 
     if (savedAccount != null) 
     { 
      App.auth.Account = savedAccount; 
     } 
     else 
     { 
      App.auth.Completed += (sender, args) => 
      { 
       if (args.IsAuthenticated) 
       { 
        // Save the account for the future 
        store.Save(args.Account, "google"); 
       } 
      }; 

      Intent loginIntent = App.auth.GetUI(this); 
      StartActivity(loginIntent); 
     } 

     #endregion [GoogleAuthentication] 
    } 

がGoogleAuthenticatorクラスはOAuth2Authenticatorを実装しています

public class GoogleAuthenticator 
    : OAuth2Authenticator 
{ 
    public GoogleAuthenticator(string clientId, Uri callbackUri, params string[] scopes) 
     : base(
      clientId, 
      (scopes ?? Enumerable.Empty<string>()).Aggregate(string.Empty, (o, s) => o + " " + s), 
      new Uri("https://accounts.google.com/o/oauth2/auth"), 
      callbackUri, 
      null) 
    { 
     Completed += (sender, args) => 
     { 
      Account = args.Account; 
     }; 
    } 

    public Account Account 
    { 
     get; 
     set; 
    } 

    public void ApplyAuthenticationToRequest(HttpWebRequest request) 
    { 
     if (Account == null) 
      throw new InvalidOperationException("You must be authenticated to make requests"); 

     string token = Account.Properties["access_token"]; 
     string type = Account.Properties["token_type"]; 
     request.Headers[HttpRequestHeader.Authorization] = String.Format("{0} {1}", type, token); 
    } 
} 

私はパラメータapproval_prompt =力を加えるauthorizationUrlを変えてみましたaccess_type = offlineでも動作しませんでした。

Xamarin.Authを使用してrefresh_tokenを取得するにはどうすればよいですか?

ありがとうございました。

+0

私はそれについての経験はありませんが、私はこのリンクがそれについて素晴らしい説明を提供すると思います:https://lostechies.com/jimmybogard/2014/11/13/mobile-authentication-with-xamarin-auth-and- refresh-tokens /基本的に、GetInitialUrlAsyncメソッドがオーバーライドされると、RequestRefreshTokenAsyncメソッドが作成され呼び出されます。それがあなたのために働くかどうか確認してください。 –

+0

こんにちはルイス、ありがとう。私はこのリンクを見たが、問題を理解するのには役に立たなかった。 –

答えて

1

あなたはそれはあなたがアカウントオブジェクトからリフレッシュトークンを取得することができますようにあなたが戻って取得に見えるこのsample code hereを見たことがあります:

CurrentUser.Properties["refresh_token"]

現在のユーザーがhere

に設定されているので、あなたにそれをコーディングしますちょうどこと:また

Account.Properties["refresh_token"];

トンあなたはaccessTokenUrlとしてnullを渡しOAuth2Authenticatorのコンストラクタでhttps://developers.google.com/identity/protocols/OAuth2InstalledApp :彼の質問には、ドキュメントはここで見つけることができますXamarin.Auth with Google APIs: Renew credentials?

+0

こんにちはIain。あなたのお返事ありがとうございます。問題は私が** refresh_token **を得ることはないということです。 この問題について私が見たすべての回答と投稿は、私がrefresh_tokenを取得したと仮定しています。 –

0

似ています。したがって、あなたのライブラリは、リフレッシュトークンとアクセストークンのための認証コードの交換、ステップ5を通過することはできません。

関連する問題