android OAuth-2.0 google 使用 webview 登录以获取用户信息 [英] android OAuth-2.0 google login for user info using webview

查看:151
本文介绍了android OAuth-2.0 google 使用 webview 登录以获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建允许用户使用 facebook 或 google 帐户登录的应用程序.他们按下登录"按钮,然后他们被要求使用 Facebook 或谷歌登录,当他们选择其中之一时,webview 会弹出.

I'm creating app that will allow user to login with facebook or google account. They press button "LOGIN" and then they are asked to login using facebook or google, when they choose one of these the webview pop up.

问题在于谷歌身份验证,阅读了一些文章并搜索了网络,仍然可以在没有客户经理的情况下进行谷歌登录,我知道他们改变了一些东西.

Problem is with google autentication, read some articles and searched the web, is still possible to make google login without Account Manager, I know that they changed something.

如果可能的话,也许可以提供一些我可以处理或看到它工作的示例.我试过这个:http://blog.doityourselfandroid.com/2011/08/06/oauth-2-0-flow-android/刚刚编辑的不是纬度而是用户信息,总是收到错误 400.

If it's possible maybe some example on which I can work on or see it working. I tried with this: http://blog.doityourselfandroid.com/2011/08/06/oauth-2-0-flow-android/ Just edited not to do latitude but user info and always getting error 400.

感谢您的帮助,到目前为止你们都帮助了很多!

Thanks for helping, you all helped a lot so far!

关于错误:我收到 exeption 400 bad request 我一般知道哪一行和什么问题.

edit: About error: I got exeption 400 bad request I generaly know which line and what is problem.

问题出在我创建 AccessTonekResponse 并执行的地方,因为它需要客户端密码,而我无法在 google api 页面上仅在 Web 应用程序中找到客户端密码,在已安装的应用程序中没有任何内容.

Problem is in line where I create AccessTonekResponse and execute because it require client secret and I cant find client secret on google api page only in web app nothing in installed app.

        public void onPageStarted(WebView view, String url,Bitmap bitmap)  {  
            System.out.println("onPageStarted : " + url);
        }
        @Override  
        public void onPageFinished(WebView view, String url)  {  

            if (url.startsWith(OAuth2ClientCredentials.REDIRECT_URI)) {
                try {

                    if (url.indexOf("code=")!=-1) {

                        String code = extractCodeFromUrl(url);
                        System.out.println(code);
                          AccessTokenResponse accessTokenResponse = new GoogleAuthorizationCodeGrant(new NetHttpTransport(),
                                          new JacksonFactory(),
                                          OAuth2ClientCredentials.CLIENT_ID,
                                          OAuth2ClientCredentials.CLIENT_SECRET,
                                          code,
                                          OAuth2ClientCredentials.REDIRECT_URI).execute();

                          CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
                          credentialStore.write(accessTokenResponse);
                          view.setVisibility(View.INVISIBLE);
                          startActivity(new Intent(OAuthAccessTokenActivity.this,LatitudeApiSample.class));
                    } else if (url.indexOf("error=")!=-1) {
                        view.setVisibility(View.INVISIBLE);
                        new SharedPreferencesCredentialStore(prefs).clearCredentials();
                        startActivity(new Intent(OAuthAccessTokenActivity.this,LatitudeApiSample.class));
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println(e.getLocalizedMessage());
                }

            }
            System.out.println("onPageFinished : " + url);

        }

错误代码:

08-22 09:22:07.866: W/System.err(391): com.google.api.client.http.HttpResponseException: 400 Bad Request
08-22 09:22:07.866: W/System.err(391):  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:380)
08-22 09:22:07.876: W/System.err(391):  at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.executeUnparsed(AccessTokenRequest.java:457)
08-22 09:22:07.876: W/System.err(391):  at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.execute(AccessTokenRequest.java:473)
08-22 09:22:07.876: W/System.err(391):  at com.ecs.android.sample.oauth2.OAuthAccessTokenActivity$1.onPageFinished(OAuthAccessTokenActivity.java:79)
08-22 09:22:07.876: W/System.err(391):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:274)
08-22 09:22:07.896: W/System.err(391):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 09:22:07.896: W/System.err(391):  at android.os.Looper.loop(Looper.java:123)
08-22 09:22:07.896: W/System.err(391):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-22 09:22:07.896: W/System.err(391):  at java.lang.reflect.Method.invokeNative(Native Method)
08-22 09:22:07.896: W/System.err(391):  at java.lang.reflect.Method.invoke(Method.java:521)
08-22 09:22:07.896: W/System.err(391):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-22 09:22:07.896: W/System.err(391):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-22 09:22:07.896: W/System.err(391):  at dalvik.system.NativeStart.main(Native Method)

推荐答案

您可能在 Google 的 API 控制台作为已安装的应用程序类型为 Android.这仅适用于 AuthenticationManager.如果您想自己实施流程,请使用应用程序类型其他,您将获得所需的客户端密钥.

You probably created your client ID in Google's API Console as Installed Application with Application Type Android. This is only meant for use with AuthenticationManager. If you want to implement the flow yourself, use Application Type Other an you'll get the client secret you need.

这篇关于android OAuth-2.0 google 使用 webview 登录以获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆