簡體   English   中英

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

[英]Why doesn't gmail oauth work in my Android app?

我正在開發一個應用程序,該應用程序進行 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

由於此日志並沒有真正給出錯誤,因此我不確定可能出了什么問題。

有沒有人知道什么可能是錯誤的或者我如何調試它? 歡迎所有提示!

您的代碼很可能因為您正在使用的 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" />

如果這不能解決它,那么手頭很可能有一個更大的問題。

根據這篇文章,最好使用onCreate()進行任何非圖形初始化,因為它在onCreateView()之前被調用。 為了能夠登錄到谷歌,因為在陳述這個職位,你首先需要從像這樣的設備(把這個在您選擇一個帳戶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);
}

然后,一旦您驗證了 Token 和帳戶,您就可以使用 OAuth2 登錄 google(有關驗證代碼,請參閱OAuth2 GitHub 源代碼)。

如果您還有問題和/或我沒有完全回答你的問題,看看 如何與使用的OAuth2 AccountManager和API調用,或如何使用OAuth2和WebView 后者可能更符合您的需求。 兩者都提供了有關如何執行所需操作的完整示例文件。

如果即使閱讀了上面的兩個鏈接,您仍然需要幫助或有問題或疑慮,請隨時發表評論!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM