我正在開發一個應用程序,該應用程序可以執行oauth2身份驗證,但不幸的是,該身份驗證不再有效。據我所知(但不是100%肯定)代碼沒有任何改變,所以我不知道爲什麼它不會工作了。爲什麼gmail oauth不能在我的Android應用程序中工作?
該應用程序創建一個web視圖,並從我們的服務器,重定向至Google對這個網址(只是改變了客戶端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<mpl=popup&oauth=1&sarp=1&scc=1
從我們的iOS應用程序使用相同的系統,它像一個魅力。所以我們的服務器實現沒有任何問題。 將webview重定向到google之後,它會自動返回到應用程序,而不顯示任何Google屏幕。 我使用下面的代碼來打開網頁視圖:
@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: [email protected] 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<mpl=popup&oauth=1&sarp=1&scc=1
02-29 18:56:39.283 27510-27510/com.example I/Timeline: Timeline: Activity_idle id: [email protected] 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
由於該日誌並沒有真正提供一個錯誤,我不能確定什麼可能是錯的。
有沒有人有任何想法什麼可能是錯的,或者我該如何調試?歡迎所有提示!
也許這將有助於:http://stackoverflow.com/questions/12854468/google-oauth-api-not-working-anymore-404-error – domax
只是一個瘋狂的猜測:這可能是因爲你已經允許訪問要記住和谷歌成功驗證基於您的設備上註冊的谷歌帳戶?嘗試移除設備上的Google帳戶,清除應用中的數據並重試。 – Codebender
使用來自Facebook的Stetho http://facebook.github.io/stetho/來監控您的應用與服務器和Google服務器之間的通信。 –