为什么gmail oauth在我的Android应用程序中不起作用?

问题描述 投票:9回答:2

我正在开发一个执行oauth2身份验证的应用程序,该应用程序以前可以正常工作,但不幸的是,它不再有效。据我所知(但不是100%肯定),代码没有任何变化,所以我不知道为什么它不再起作用。

该应用程序创建了一个Webview并从我们的服务器加载一个URL,它将其重定向到google以对该URL进行身份验证(只是更改了客户端ID和我的域):

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=1234567890-XXXXXXX.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Fexample.com%3A5000%2Fchannel%2Fgmail%2Fcallback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&access_type=offline

立即将其重定向到:

https://accounts.google.com/ServiceLogin?passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?access_type%3Doffline%26scope%3Dhttps://www.googleapis.com/auth/userinfo.email%2Bhttps://www.googleapis.com/auth/gmail.readonly%26response_type%3Dcode%26redirect_uri%3Dhttp://example.com:5000/channel/gmail/callback%26client_id%3D123456789-XXXXX.apps.googleusercontent.com%26hl%3Dnl%26from_login%3D1%26as%3D-2178738b5063e716&ltmpl=popup&oauth=1&sarp=1&scc=1

我们的iOS应用中使用了相同的系统,其工作方式就像一个超级按钮。因此,我们的服务器实现没有任何问题。将Webview重定向到Google后,它会自动返回应用程序,而不会显示任何Google屏幕。我正在使用以下代码打开webview:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_browser_webview, container, false);

    webView = (WebView) view.findViewById(R.id.web_view);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setDomStorageEnabled(true);

    webView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.wtf("ERROR", description + " " + failingUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.wtf("WEBVIEW URL", url);
            if (url.contains(Api.API_ENTER_POINT)) {
                // We never actually get here
                getActivity().finish();
            }

            return false; //Allow WebView to load url
        }
    });
    if (userId != null & userToken != null) {
        Log.d("Gmail login", String.format(Api.API_GMAIL,userId,userToken));
        webView.loadUrl(String.format(Api.API_GMAIL,userId,userToken));
    }
    return view;
}

logcat输出如下:

02-29 18:56:39.028 27510-27510/com.example D/Gmail login: http://example.com:5000/api/v1/channel/gmail/on/1/CAAV8cDYVv9wBAKDfKu7zjInpUbSxBjSiouG8iFtP2EGKjb63AOAjirFf9SepSwe62PsNt0pflwZBKs8xvoH2Y7cnOsHTC33ikbwLFgwiqmK7AtHYzo2BTZAmiDGQvCKZBSdjIR5o5zvgqSZAFiGEU10PVTnXw2fRJzukQ0VEVoZC9VrO7el8hjeg2VoVBFhb9ppPCsHYkPKRWgThKJ76VJS4K3m2X7LwZD
02-29 18:56:39.092 27510-27510/com.example D/cr_Ime: [ImeAdapter.java:358] onViewFocusChanged: gainFocus [true]
02-29 18:56:39.119 27510-27510/com.example D/cr_Ime: [ImeAdapter.java:140] onCreateInputConnection returns null.
02-29 18:56:39.162 27510-27510/com.example I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2b109b89 time:227199315
02-29 18:56:39.163 27510-27510/com.example A/WEBVIEW URL: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=1234567890-XXXXXXXXXX.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Fexample.com%3A5000%2Fchannel%2Fgmail%2Fcallback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&access_type=offline
02-29 18:56:39.216 27510-27510/com.example A/WEBVIEW URL: https://accounts.google.com/ServiceLogin?passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?access_type%3Doffline%26scope%3Dhttps://www.googleapis.com/auth/userinfo.email%2Bhttps://www.googleapis.com/auth/gmail.readonly%26response_type%3Dcode%26redirect_uri%3Dhttp://example.com:5000/channel/gmail/callback%26client_id%3D1234567890-XXXXXXXXXX.apps.googleusercontent.com%26hl%3Dnl%26from_login%3D1%26as%3D-231b0767e02a8ca9&ltmpl=popup&oauth=1&sarp=1&scc=1
02-29 18:56:39.283 27510-27510/com.example I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@16bf8d10 time:227199436
02-29 18:56:39.287 27510-27510/com.example D/cr_Ime: [ImeAdapter.java:358] onViewFocusChanged: gainFocus [false]
02-29 18:56:39.287 27510-27510/com.example D/cr_Ime: [ImeAdapter.java:326] hideKeyboard
02-29 18:56:39.288 27510-27510/com.example D/cr_Ime: [InputMethodManagerWrapper.java:56] isActive: false

由于此日志并未真正给出错误,所以我不确定可能是什么错误。

有人知道什么可能是错误的,或者我该如何调试呢?欢迎所有提示!

java android oauth google-oauth
2个回答
1
投票

您的代码很可能由于您正在使用的API的某些部分中的更新而停止工作;我猜它可能正在从OAuth更新到OAuth2,或者可能是简单的补丁更新。最简单的解决方法是向AndroidManifest.xml中添加使用权限和元数据:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

如果不能解决问题,就可以解决,那么很可能会遇到更大的问题。

根据this post,最好使用onCreate()进行任何非图形初始化,因为它是在onCreateView()之前调用的。为了能够登录到Google,如this post中所述,您首先需要从设备中选择一个这样的帐户(将其放入Main.javaMainActivity.java中):

public static AccountManager accountManager;
accountManager = AccountManager.get(this);
Account[] accounts = accountManager.getAccountsByType("com.google");

然后,您需要像这样从选定的帐户中获取令牌:

private void onAccountSelected(final Account account) {
    accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, this, new AccountManagerCallback<Bundle>() {
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                useToken(account, token);
            } catch (OperationCanceledException e) {
                onAccessDenied();
            } catch (Exception e) {
                handleException(e);
            }
        }
    }, null);
}

然后,您在对令牌和帐户进行身份验证之后,就可以使用OAuth2登录到Google(身份验证代码请参考OAuth2 GitHub Source

如果您仍然遇到问题和/或我没有完全回答您的问题,请参阅this,了解如何将OAuth2与AccountManager和API调用或[C0有关如何在this中使用OAuth2的信息。后者可能更符合您的需求。两者都提供了有关如何执行所需操作的完整示例文件。

如果即使在阅读了上面的两个链接之后仍然] >>您仍然需要帮助或有疑问或疑虑,请随时发表评论!

您有一个Gmail帐户吗?还是假设您有一个Gmail帐户,您知道如何登录到该帐户吗?...如果您有Gmail帐户并且不知道如何登录,请参阅WebView


-1
投票

您有一个Gmail帐户吗?还是假设您有一个Gmail帐户,您知道如何登录到该帐户吗?...如果您有Gmail帐户并且不知道如何登录,请参阅WebView

© www.soinside.com 2019 - 2024. All rights reserved.