2016-02-29 50 views
9

我正在开发一个应用程序,该应用程序可以执行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&ltmpl=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&ltmpl=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 

由于该日志并没有真正提供一个错误,我不能确定什么可能是错的。

有没有人有任何想法什么可能是错的,或者我该如何调试?欢迎所有提示!

+0

也许这将有助于:http://stackoverflow.com/questions/12854468/google-oauth-api-not-working-anymore-404-error – domax

+0

只是一个疯狂的猜测:这可能是因为你已经允许访问要记住和谷歌成功验证基于您的设备上注册的谷歌帐户?尝试移除设备上的Google帐户,清除应用中的数据并重试。 – Codebender

+0

使用来自Facebook的Stetho http://facebook.github.io/stetho/来监控您的应用与服务器和Google服务器之间的通信。 –

回答

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()之前调用。为了能够登录到谷歌,如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(请参阅OAuth2 GitHub Source的验证码),以谷歌。

如果您仍然遇到的问题和/或我没有完全回答你的问题,看看如何使用OAuth2和如何使用OAuth2和WebViewAccountManager和API调用,或thisthis 。后者可能更适合您的需求。两者都提供完整的示例文件,说明如何执行所需的操作。

如果连阅读上述您仍然需要帮助或有疑问或顾虑,随意发表评论两个环节后!

相关问题