Xamarin android GoogleAccountCredential

时间:2018-04-28 22:32:18

标签: xamarin xamarin.android google-signin

我正在尝试使用Xamarin中的Google登录来创建YoutubeService实例。目前,我可以通过此功能获得GoogleSignInAccount:

    public void Login()
    {
        if(account != null)
        {
            CreateYoutube();
            return;
        }

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                .RequestServerAuthCode("112086459272-o965mju8hjqqr1fq333g3am9hqrm650e.apps.googleusercontent.com")
                .RequestEmail()
                .RequestScopes(new Scope(YouTubeService.Scope.Youtube))
                .Build();

        googleClient = new GoogleApiClient.Builder(this)
                .EnableAutoManage(this, this)
                .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .Build();

        OptionalPendingResult silentLog = Auth.GoogleSignInApi.SilentSignIn(googleClient);
        if (silentLog.IsDone)
        {
            Console.WriteLine("&Casting silentlog to google account");
            GoogleSignInResult result = (GoogleSignInResult)silentLog.Get();
            if (result.IsSuccess)
            {
                account = result.SignInAccount;
                CreateYoutube();
            }
        }

        StartActivityForResult(Auth.GoogleSignInApi.GetSignInIntent(googleClient), 1598);
    }

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if(requestCode == 1598)
        {
            GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
            if (result.IsSuccess)
            {
                account = result.SignInAccount;
                CreateYoutube();
            }
        }
    }

它运行正常,我可以获得用户的个人资料,但我发现创建REST Api实例(如youtube)的唯一方法是使用GoogleAccountCredential类(来自此处的文档:https://developers.google.com/android/guides/http-auth) 。这是我想要使用的代码:

    void CreateYoutube()
    {
        GoogleAccountCredential credential = GoogleAccountCredential.UsingOAuth2(
                                this /*Context*/,
                                YouTubeService.Scope.Youtube);
        credential.SelectedAccount = account;

        YouTubeService service = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential
        });
    }

但是我找不到在Xamarin中包含GoogleAccountCredential的api。有没有其他方法可以使用覆盖GoogleAccountCredential类的oauth或api创建YoutubeService?

如果你对我的问题不理解,请不要犹豫。 谢谢阅读, Tristan Roux

1 个答案:

答案 0 :(得分:1)

  

在Xamarin

中找不到包含GoogleAccountCredential的API

您正在寻找Nuget包:Xamarin.GooglePlayServices.Auth

示例登录:

~~~    
// mGoogleApiClient is a fully configured GoogleApiClient with the scopes your app need
var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
StartActivityForResult(signInIntent, 1010);
~~~

现在,在OnActivityResult中,您可以从收到的意图中检索GoogleSignInAccount

示例OnActivityResultGoogleSignInAccount

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (requestCode == 1010)
    {
        var result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
        GoogleSignInAccount account = result.SignInAccount;
        /// Do something with your "GoogleSignInAccount"
    }
}