2016-10-27 53 views
2

我用完了我的方式。我遵循tutorial(第1部分和第2部分)在我的android項目中使用Google People API。它總是在簽名的apk中出現parameter code is missing的錯誤,並且在得到GoogleTokenResponse時出錯。它在調試版本中工作正常。我做錯了什麼?Android Google人員API錯誤:TokenResponseException:缺少參數:代碼

而且我已經做了所有這些:

  • 更改SHA1來發布的版本。
  • 替換從Firebase下載的已下載的google-services.json文件。
  • 使用OAuth的客戶端ID和客戶端密鑰網絡客戶端ID
  • 使緩存失效/重新啓動。
  • 刪除並重新創建憑證。

enter image description here

enter image description here

谷歌登錄類:

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
if (result.isSuccess()) { 
      new PeoplesAsync().execute(result); 
     } 

PeoplesAsync:成功登錄後

public void Google_signIn() { 

     //migrate to people api 
     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       // The serverClientId is an OAuth 2.0 web client ID 
       .requestServerAuthCode(Constants.Google_Client_ID) 
       .requestEmail() 
       .requestScopes(new Scope(Scopes.PLUS_LOGIN), 
         new Scope(PeopleScopes.USERINFO_EMAIL)) 
       .build(); 

     if (mGoogleApiClient == null) { 

      mGoogleApiClient = new GoogleApiClient.Builder(activity) 
        .enableAutoManage(activity /* FragmentActivity */, this /* OnConnectionFailedListener */) 
        .addOnConnectionFailedListener(this) 
        .addConnectionCallbacks(this) 
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
        .addApi(LocationServices.API) 
        .build(); 
     } 

     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     activity.startActivityForResult(signInIntent, GOOGLE_SIGN_IN); 
    } 

處理器結果

class PeoplesAsync extends AsyncTask<GoogleSignInResult, Void, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected String doInBackground(GoogleSignInResult... params) { 

      GoogleSignInAccount acct = params[0].getSignInAccount(); 
      JSONObject userInfo = new JSONObject(); 

      try { 

       //basic profile information 
       String G_id = acct.getId(); 
       String G_email = acct.getEmail(); 
       String G_name = acct.getDisplayName(); 
       String G_url = acct.getPhotoUrl().toString();//profile img 

       userInfo.put(AccountCenter.ACC_ID, G_id); 
       userInfo.put(AccountCenter.ACC_NAME, G_name); 
       userInfo.put(AccountCenter.ACC_EMAIL, G_email); 
       userInfo.put(AccountCenter.ACC_PROFILE_IMAGE, G_url); 

       //problem here 
       //additional profile information 
       com.google.api.services.people.v1.People peopleService = PeopleHelper.setUp(activity, acct.getServerAuthCode()); 

       com.google.api.services.people.v1.model.Person profile = peopleService.people().get("people/me") 
         .setRequestMaskIncludeField("person.genders,person.urls,person.birthdays").execute(); 

       if (!profile.isEmpty()) { 

        List<Url> urls = profile.getUrls(); 
        List<Gender> genders = profile.getGenders(); 
        List<Birthday> birthdays = profile.getBirthdays(); 

        String G_link = urls.get(0).getValue(); 
        String G_gender = genders.get(0).getValue(); 
        Date bday = birthdays.get(0).getDate(); 
        String G_birthday = String.format("%02d", bday.getDay()) + "-" + String.format("%02d", bday.getMonth()) + "-" + String.format("%04d", bday.getYear());// check: year will be 0000 if not shown, can be null ? 

        userInfo.put(AccountCenter.ACC_URL, G_link); 
        userInfo.put(AccountCenter.ACC_GENDER, G_gender); 
        userInfo.put(AccountCenter.ACC_BIRTHDAY, G_birthday); 
       } 

       AccountCenter.setUserInfo(activity, userInfo.toString(), Constants.ACCOUNT_TYPE_GOOGLE); 
       memberSocialLogin(activity, userInfo, Constants.ACCOUNT_TYPE_GOOGLE, Constants.PROVIDER_NAME_GOOGLE); 

      } catch (IOException ie) { 
       ie.printStackTrace(); 
       return ie.toString(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       return e.toString(); 
      } 

      return "1"; 
     } 

     @Override 
     protected void onPostExecute(String response) { 
      super.onPostExecute(response); 

      if (!response.equals("1")) { 
       GeneralMethods.hideProgressDialog(); 

       if (mGoogleSignedInListener != null) { 
        mGoogleSignedInListener.onError(response); 
       } 
      } 
     } 
    } 

PeopleHelper:

public class PeopleHelper { 

    private static final String APPLICATION_NAME = "Grapps";//any name 

    public static People setUp(final Context context, String serverAuthCode) { 

     String progress = "0%"; 

     try { 

      HttpTransport httpTransport = new NetHttpTransport(); 
      JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 

      // Redirect URL for web based applications. 
      // Can be empty too. 
      String redirectUrl = ""; 

      progress = "10%"; 

      //problem here 
      // Exchange auth code for access token 
      GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
        httpTransport, 
        jsonFactory, 
        Constants.Google_Client_ID, 
        Constants.Google_Client_Secret, 
        serverAuthCode, 
        redirectUrl).execute(); 

      progress = "40%"; 

      // Create a GoogleCredential object using the tokens from GoogleTokenResponse 
      GoogleCredential credential = new GoogleCredential.Builder() 
        .setClientSecrets(Constants.Google_Client_ID, Constants.Google_Client_Secret) 
        .setTransport(httpTransport) 
        .setJsonFactory(jsonFactory) 
        .build(); 

      progress = "60%"; 

      credential.setFromTokenResponse(tokenResponse); 

      progress = "80%"; 

      // credential can then be used to access Google services 
      return new People.Builder(httpTransport, jsonFactory, credential) 
        .setApplicationName(APPLICATION_NAME) 
        .build(); 

     } catch (IOException e) { 

      final String finalProgress = progress + "\n" + e.toString(); 

      Handler handler = new Handler(context.getMainLooper()); 
      handler.post(new Runnable() { 
       public void run() { 
        GeneralMethods.showAlert(context, "Error setting up People", finalProgress); 
       } 
      }); 

      return null; 
     } 
    } 

} 

回答

1

如果沒有在谷歌控制檯和密碼的問題,請看看Android項目設置,將minifyEnabled中的build.gradle可能有助於假的。

buildTypes { 
    release { 
//   minifyEnabled true 
//   shrinkResources true 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
+0

確切!不知道爲什麼,但'minifyEnabled true'確實阻止了我在已簽名的apk中獲得'GoogleTokenResponse',並且我的應用程序在將其設置爲false後完美工作。 –