2016-02-21 88 views
-1

我打字谷歌加登錄集成在我application.The的問題是,交方法返回Bad Request 400狀態,當我嘗試後成功取them.What的原因可能推用戶憑據服務器它!任何形式的幫助將不勝感激!壞請求400個谷歌加登錄

我已經複製粘貼下面的代碼:

public class GooglePlusLoginActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 

    private Context mContext; 
    private static final String TAG = GooglePlusLoginActivity.class.getSimpleName(); 

    /* Is there a ConnectionResult resolution in progress? */ 
    private boolean mIsResolving = false; 

    /* Should we automatically resolve ConnectionResults when possible? */ 
    private boolean mShouldResolve = false; 
    private GoogleApiClient mGoogleApiClient; 
    private static final int RC_SIGN_IN = 3434; 
    private ProgressDialog mProgressDialog; 

    // private SocialLoginService mSocialLoginService; 
    private String mAccountName; 
    private String mToken; 
    private String fullName = null; 
    private String email = null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mContext = this; 


     if (mGoogleApiClient != null && GoogleApiClient.isConnected()) { 
      Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); 
      mGoogleApiClient.disconnect(); 
      mGoogleApiClient.connect(); 
     } 

     Plus.PlusOptions options = Plus.PlusOptions.builder().build(); 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .enableAutoManage(this, this) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, LoginFragment.gso) 
       .addScope(new Scope(Scopes.PLUS_LOGIN)) 
       .addScope(new Scope(Scopes.PLUS_ME)) 
       .addScope(new Scope(Scopes.PROFILE)) 
       .addApi(Plus.API, options) 
       .addScope(Plus.SCOPE_PLUS_LOGIN) 
       .addApi(Plus.API) 
       .build(); 


     // User clicked the sign-in button, so begin the sign-in process and automatically 
     // attempt to resolve any errors that occur. 
     mShouldResolve = true; 
     mGoogleApiClient.connect(); 
     signIn(); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     // Could not connect to Google Play Services. The user needs to select an account, 
     // grant permissions or resolve an error in order to sign in. Refer to the javadoc for 
     // ConnectionResult to see possible error codes. 
     Log.d(TAG, "onConnectionFailed:" + connectionResult); 

     if (!mIsResolving && mShouldResolve) { 
      if (connectionResult.hasResolution()) { 
       try { 
        connectionResult.startResolutionForResult(this, RC_SIGN_IN); 
        mIsResolving = true; 
       } catch (IntentSender.SendIntentException e) { 
        Log.e(TAG, "Could not resolve ConnectionResult.", e); 
        mIsResolving = false; 
        mGoogleApiClient.connect(); 
       } 
      } else { 
       // Could not resolve the connection result, show the user an 
       // error dialog. 
       dismissDialog(); 
       Util.warningDialog(mContext, "Error occured during Google login. Try again"); 
       GooglePlusLoginActivity.this.finish(); 

      } 
     } else { 
      //show signout 
      GooglePlusLoginActivity.this.finish(); 
     } 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Log.d(TAG, "onActivityResult:" + requestCode + " intent data=" + data); 
     Log.d(TAG, "intent data1="+data); 

     if (requestCode == RC_SIGN_IN) { 
      // If the error resolution was not successful we should not resolve further. 
      new GetAccessTokenTask(data).execute(); 
      if (resultCode != RESULT_OK) { 
       mShouldResolve = false; 
      } 
      mIsResolving = false; 
      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     mGoogleApiClient.disconnect(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     // onConnected indicates that an account was selected on the device, that the selected 
     // account has granted any requested permissions to our app and that we were able to 
     // establish a service connection to Google Play services. 

     Log.v(TAG, "google i am connected...."); 
     mShouldResolve = false; 
    } 


    @Override 
    public void onConnectionSuspended(int i) { 
     exitCurrentActivity(); 
    } 

    class GetAccessTokenTask extends AsyncTask<Void, Void, String> { 
     private Intent mData; 
     public GetAccessTokenTask(Intent data) { 
      this.mData = data ; 
      Log.d(TAG, "intent data="+mData); 

     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog("Fetching profile ..."); 
     } 

     @Override 
     protected void onPostExecute(String token) { 
      super.onPostExecute(token); 
      dismissDialog(); 
      checkForTokenEmpty(); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient); 
      Log.v(TAG,"account name="+mAccountName); 
      final String SCOPES = "https://www.googleapis.com/auth/plus.profile.emails.read"; 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(mData); 
      GoogleSignInAccount acct = result.getSignInAccount(); 
      if(acct!= null) 
       Log.v(TAG," result success"+acct.getEmail()); 
      if (result.isSuccess() && acct != null) { 
       fullName = acct.getDisplayName(); 
       email = acct.getEmail(); 
       Log.v(TAG, acct.getDisplayName()); 
       try { 
        mToken = GoogleAuthUtil.getToken(mContext, mAccountName, "oauth2:" + SCOPES); 
       } catch (IOException | GoogleAuthException e) { 
        e.printStackTrace(); 
       } 
      } 
      Plus.PeopleApi.load(mGoogleApiClient, acct != null ? acct.getId() : null) 
        .setResultCallback(new ResultCallback<People.LoadPeopleResult>() { 
         @Override 
         public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) { 
          Person person = loadPeopleResult.getPersonBuffer().get(0); 
          String photoUrl = person.getImage().getUrl(); 
          photoUrl = photoUrl.substring(0, photoUrl.length() - 2) + 200; 
          Prefs.putString(Constants.PREFS_GOOGLE_PROFILE_IMAGE, photoUrl); 
          Log.d(TAG, "photo url:" + photoUrl); 

         } 
        }); 
      //then send to server 

      Log.v(TAG,"token="+mToken); 
      return mToken; 

     } 


     /** 
     * check for token empty 
     */ 
     private void checkForTokenEmpty() { 
      if (!TextUtils.isEmpty(mToken)) 
       fetchGoogleProfile(); //if not empty fetch profile detail 
      else{ 
       Log.v(TAG," token empty"); 
       exitCurrentActivity(); 

      } 
     } 
    } 

    private void exitCurrentActivity() { 
     EventBus.getDefault().post(new SocialLoginEvent(false)); 
     GooglePlusLoginActivity.this.finish(); 
    } 

    /** 
    * fetch google profile details 
    */ 


    private void showDialog(String message) { 
     mProgressDialog = new ProgressDialog(mContext); 
     mProgressDialog.showProgress(); 
     if (message.isEmpty()) 
      mProgressDialog.setMessage("Please wait ..."); 
     else 
      mProgressDialog.setMessage(message); 
    } 

    private void dismissDialog() { 
     if (mProgressDialog != null) 
      mProgressDialog.dismissDialog(); 
    } 

    private void fetchGoogleProfile() { 
     Log.v(TAG,"sending google plus profile to server"); 
     SocialLogin socialLogin = new SocialLogin(email, fullName, fullName, SocialLoginType.GOOGLE, mToken); 
     EventBus.getDefault().post(new SocialLoginEvent(true, socialLogin)); 
     GooglePlusLoginActivity.this.finish(); 

    } 

    private void signIn() { 
     showDialog("please wait...."); 
     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     startActivityForResult(signInIntent, RC_SIGN_IN); 
    } 
} 

此類顯示bad request 400發佈憑據服務器時。

public class SocialLoginService { 

    private static final String TAG = SocialLoginService.class.getSimpleName(); 
    public SocialLoginHttpListener mSocialLoginHttpListener; 
    private SocialLoginType mSocialLoginType; 

    public void postSocialToken(SocialLogin socialLogin) { 
     RequestParams params = new RequestParams(); 
     params.put("UserName", socialLogin.getUserName()); 
     params.put("Email", socialLogin.getEmail()); 
     params.put("FullName", socialLogin.getFullName()); 
     params.put("Platform", "Android"); 

     String providerType = "Google"; 
     mSocialLoginType = SocialLoginType.GOOGLE; 
     if (socialLogin.getSocialLoginType() == SocialLoginType.FACEBOOK) { 
      providerType = "Facebook"; 
      mSocialLoginType = SocialLoginType.FACEBOOK; 
     } 
     params.put("Provider", providerType); 

     params.put("ExternalAccessToken", socialLogin.getToken()); 
     Log.v(TAG, "social login post data: " + params.toString()); //This tags showing correct information. 

     RestClient.post(" valid url here", params, new AsyncHttpResponseHandler() { 
      @Override 
      public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 
       try { 
        String response = new String(responseBody); 
        Log.v(TAG, "social login success: " + response); 
        JSONObject object = new JSONObject(response); 
        String accessToken = object.getString("access_token"); 
        String token_type = object.getString("token_type"); 
        double expireTime = object.getDouble("expires_in"); 
        if (!accessToken.isEmpty()) { 
         Util.setAccessToken(accessToken); 
         Util.setTokenType(token_type); 
         Util.setTokenExpireDate(expireTime); 
         Util.setLoginStatus(true); 
        } 
        mSocialLoginHttpListener.setSocialLoginStatus(true, mSocialLoginType); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
        mSocialLoginHttpListener.setSocialLoginStatus(false, mSocialLoginType); 
       } 
      } 

      @Override 
      public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 
       Log.v(TAG, "social login failure: " + error.getMessage() + " " + statusCode); 
       error.printStackTrace(); 
       error.getCause(); 
       mSocialLoginHttpListener.setSocialLoginStatus(false, mSocialLoginType); 

      } 
     }); 
    } 
} 

這是日誌貓:

02-21 15:51:11.227 1111-1111/com.app.food V/AsyncHttpClient: Beware! Using the fix is insecure, as it doesn't verify SSL certificates. 
02-21 15:51:17.106 1111-2690/com.app.food V/AsyncHttpClient: Headers were overwritten! (Content-Type | application/x-www-form-urlencoded) overwrites (Content-Type | application/x-www-form-urlencoded; charset=UTF-8) 
02-21 15:51:17.439 1111-1111/com.app.food V/AsyncHttpRH: Progress 55 from 55 (100%) 
02-21 15:51:17.439 1111-1111/com.app.food V/SocialLoginService: social login failure: Bad Request 400 
02-21 15:51:17.439 1111-1111/com.app.food W/System.err: cz.msebera.android.httpclient.client.HttpResponseException: Bad Request 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:446) 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:160) 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177) 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106) 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
02-21 15:51:17.447 1111-1111/com.app.food W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
02-21 15:51:17.448 1111-1111/com.app.food W/System.err:  at java.lang.Thread.run(Thread.java:818) 
02-21 16:05:49.482 1111-1111/com.app.food W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
02-21 16:05:49.483 1111-1111/com.app.food W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
02-21 16:05:49.485 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.488 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.506 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.508 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.509 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.512 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.512 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.513 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.514 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:05:49.514 1111-1111/com.app.food W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
02-21 16:05:49.515 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.560 1111-1111/com.app.food W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
02-21 16:08:47.562 1111-1111/com.app.food W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
02-21 16:08:47.563 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.565 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.574 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.575 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.576 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.577 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.578 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.580 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.581 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
02-21 16:08:47.582 1111-1111/com.app.food W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
02-21 16:08:47.582 1111-1111/com.app.food W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 

我已經使用了相同的SocialLoginService類做同樣爲Facebook也就是獲取用戶憑據有點兒工作,它發佈到服務器和它的工作完美。不知道爲什麼它顯示在谷歌加的情況下錯誤。

回答

0

解決了這個問題!我在Google開發者控制檯中用不同的指紋註冊項目時,忘記了更新服務器中使用的舊client Id。因此,服務器被扔出來bad request 400每當我試圖使從谷歌獲取加上傳用戶憑據。