【问题标题】:Google OAuth 2 - How to get refresh token using Xamarin.Auth?Google OAuth 2 - 如何使用 Xamarin.Auth 获取刷新令牌?
【发布时间】:2017-08-24 16:34:35
【问题描述】:

我正在使用 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=forceaccess_type=offline 但也没有用。

如何使用 Xamarin.Auth 获取 refresh_token?

谢谢。

【问题讨论】:

  • 我没有这方面的经验,但我认为这个链接提供了一个很好的解释:lostechies.com/jimmybogard/2014/11/13/… 基本上,GetInitialUrlAsync 方法被覆盖,一个 RequestRefreshTokenAsync 方法被创建和调用。检查它是否适合您。
  • 嗨,路易斯,谢谢。我看到了这个链接,但它并没有帮助我理解问题。

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


【解决方案1】:

你有没有看到这个sample code here 看起来你可以从你取回的帐户对象中获取刷新令牌:

CurrentUser.Properties["refresh_token"]

当前用户设置为here

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

Account.Properties["refresh_token"];

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

【讨论】:

  • 嗨伊恩。感谢您的回复和帮助。问题是我从来没有得到 refresh_token。我看到的关于这个问题的所有答案和帖子都假设我得到了一个 refresh_token。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-05
    • 2015-11-26
    • 1970-01-01
    • 2017-03-30
    • 2014-07-15
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多