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を取得するにはどうすればよいですか?
ありがとうございました。
私はそれについての経験はありませんが、私はこのリンクがそれについて素晴らしい説明を提供すると思います:https://lostechies.com/jimmybogard/2014/11/13/mobile-authentication-with-xamarin-auth-and- refresh-tokens /基本的に、GetInitialUrlAsyncメソッドがオーバーライドされると、RequestRefreshTokenAsyncメソッドが作成され呼び出されます。それがあなたのために働くかどうか確認してください。 –
こんにちはルイス、ありがとう。私はこのリンクを見たが、問題を理解するのには役に立たなかった。 –