使用Xamarin.Auth进行Google身份验证

时间:2014-07-20 09:19:55

标签: android oauth-2.0 xamarin xamarin.android

我正在使用Xamarin.Android开发一个应用程序。

我需要启用Facebook和Google登录,我选择使用Xamarin.Auth组件。 我让它与Facebook合作,但我在谷歌遇到了一些问题。

这是我的代码:

        var auth = new OAuth2Authenticator (
            clientId: "myId.apps.googleusercontent.com",
            clientSecret: "mysecred",
            scope: "openid",
            authorizeUrl: new Uri ("https://accounts.google.com/o/oauth2/auth"),
            redirectUrl: new Uri ("myredirect:oob"),
            accessTokenUrl: new Uri ("https://accounts.google.com/o/oauth2/token"),
            getUsernameAsync: null
        ); 

        activity.StartActivity (auth.GetUI (activity));

        auth.Completed += (sender, e) => {
            Console.WriteLine (e.IsAuthenticated);
        };

这样就显示了Google Activity,我可以插入我的用户名和密码。但是,当我点击谷歌登录按钮时,我收到这样的消息:

  

google auth请将此代码切换复制到您的应用程序并将其粘贴到[code]

我与该代码有什么关系?我只需要获取用户名/ lastname / email / id。

非常感谢!

1 个答案:

答案 0 :(得分:3)

贝娄的步骤对我有用。

1.在Google开发者控制台中注册为webapplication而不是已安装的应用程序(android)*提供带有效网址的重定向网址(“http://abcd.com/xyz.aspx”)应在应用程序代码中使用。

2.在身份验证完成后,它将返回access_token

3.使用access_token发出REST请求以获取用户完整信息(https://www.googleapis.com/oauth2/v1/userinfo?access_token=“+ accessTokenValue +”。)

4.反序列化json响应以获取object中的信息。

检查源代码:http://appliedcodelog.blogspot.in/2015/08/login-by-google-account-integration-for.html