android OAuth-2.0 google login for user info using webview

半世苍凉 提交于 2019-12-06 06:51:30

问题


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.

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!

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

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);

        }

error code:

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)

回答1:


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.



来源:https://stackoverflow.com/questions/12063097/android-oauth-2-0-google-login-for-user-info-using-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!