2016-02-29 16 views
9

私は働いていたoauth2認証を行うアプリケーションに取り組んでいますが、残念ながらこれ以上は動作しません。私が知っている限り(100%は確信していません)、コード上で何も変わっていないので、なぜそれがもう動作しないのか分かりません。AndroidアプリでGmailのoauthが動作しないのはなぜですか?

アプリはWebViewのを作成し、このURLに(ちょうど変更クライアントIDと自分のドメイン)を認証するためにGoogleにリダイレクトし、当社のサーバーからURL、ロード:

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アプリ内から使用されています。だから私たちのサーバーの実装には何も問題はありません。 ウェブビューを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: [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アカウントに基づいて正常に認証されますか?端末でGoogleアカウントを削除し、アプリのデータを消去してもう一度お試しください。 – Codebender

+0

facebook http://facebook.github.io/stetho/の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によれば、onCreateView()の前に呼び出されるため、非グラフィカルな初期化を行うには、onCreate()を使用することをお勧めします。そのようなデバイスからアカウントを選択するために、あなたの最初の必要性がthis postに述べたように(あなたのMain.javaまたはMainActivity.javaでこれを入れて)、Googleにログインできるようにするために:

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 GitHub Sourceを参照してください)のOAuth2を使用してGoogleにログインすることができます。

あなたはまだ問題を抱えている、および/または私は完全に、あなたの質問に答えるAccountManagerし、API呼び出しでのOAuth2の使用方法についてのthisをチェックアウト、またはWebViewでのOAuth2を使用する方法についてthisしなかった場合。後者はおそらくあなたのニーズにもっと関係します。両方とも、あなたが探していることをやる方法について、サンプルファイル全体を提供しています。

でも上記の2つのリンクを読んだ後、あなたはまだコメントを残して自由に感じる、助けを必要とするか、質問や懸念を持っている場合は!

関連する問題