Google OAuth 2 - 如何使用Xamarin.Auth获取刷新令牌?

时间:2016-04-30 04:17:16

标签: xamarin oauth-2.0 google-api xamarin.auth

我正在使用Xamarin.Auth对用户进行身份验证,并可以访问Google Calendar API。我能够获得访问令牌并做我需要的任何事情。问题是我收到的 access_token 在一小时后过期。我在几个帖子中查找了关于此的示例和信息,但我仍然无法要求新的令牌。这是我在Xamarin.Droid项目中使用的代码:

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

我尝试更改authorizationUrl,添加参数 approval_prompt = force access_type =离线,但它也不起作用。

如何使用Xamarin.Auth获取refresh_token?

感谢。

2 个答案:

答案 0 :(得分:1)

您是否看到此sample code here看起来您可以从您获得的帐户对象中获取刷新令牌:

CurrentUser.Properties["refresh_token"]

当前用户设置为here

所以在你的代码中它只是:

Account.Properties["refresh_token"];

此问题也类似 Xamarin.Auth with Google APIs: Renew credentials?

答案 1 :(得分:0)

可以在此处找到文档:https://developers.google.com/identity/protocols/OAuth2InstalledApp 在OAuth2Authenticator的构造函数中,您将null作为accessTokenUrl传递。因此,我猜你的图书馆无法通过第5步,交换授权代码进行刷新和访问令牌。