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



我使用Xamarin.Auth对用户进行身份验证,并可以访问Google日历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,添加参数approvel_prompt=forceaccess_type=offline,但也不起作用。

如何使用Xamarin.Auth获取refresh_token?

谢谢。

你在这里看到这个示例代码了吗?看起来你可以从你得到的账户对象中获得刷新令牌:

CurrentUser.Properties["refresh_token"]

当前用户设置在此处

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

Account.Properties["refresh_token"];

这个问题也很相似Xamarin.Auth与谷歌API:续订凭据?

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

相关内容

  • 没有找到相关文章

最新更新