2016-10-04 40 views
2

爲什麼我得到這個錯誤?我已經在Google開發者控制檯中創建了一個項目,啓用了Drive API和Google+ API,並添加了我的已簽名apk的sha1(我已經在Google Play控制檯上作爲測試版共享)的「Android客戶端ID」。

我得到的權限GET_ACCOUNTS,當我選擇我的G +郵件列表中,我得到這個錯誤:G +連接失敗statusCode = SIGN_IN_REQUIRED

D/G+: Connection failed4ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent 

我必須在我的項目文件夾添加client_id.json地方? (我只是創造了它在谷歌開發者控制檯驅動API)。

代碼:

 // initialize g+ api client 
    if (Sp.getBoolean("plus_pic", false)) { 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(Drive.API) 
       .addScope(Drive.SCOPE_FILE) 
       .build(); 
    } 

和:

@Override 
public void onConnected(Bundle bundle) { 

    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { 
     Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); 

     String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient); 
     Person.Image personImage; 
     Person.Cover.CoverPhoto personCover; 

     try { 

      personImage = currentPerson.getImage(); 
      personCover = currentPerson.getCover().getCoverPhoto(); 
     } catch (Exception e) { 
      Log.e("G+ COnnection error"," ->"+e.toString()); 
      personCover = null; 
      personImage = null; 
     } 

     if (personCover != null && personImage != null) { 

      String imgUrl = personImage.getUrl(); 
      Log.e("g+ connection","cover and pic not null"); 
      // getting full size image 
      StringBuilder stringBuilder = new StringBuilder(); 
      stringBuilder.append(imgUrl); 
      stringBuilder.delete(imgUrl.length() - 6, imgUrl.length()); 
      Log.d("G+", stringBuilder.toString()); 
      mGoogleName.setText(currentPerson.getDisplayName()); 
      mGoogleId.setText(accountName); 
      // setting cover pic 
      ImageLoader.getInstance().loadImage(personCover.getUrl(), displayImageOptions, new SimpleImageLoadingListener() { 
       @Override 
       public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
        super.onLoadingComplete(imageUri, view, loadedImage); 
        drawerHeaderParent.setBackgroundColor(Color.parseColor("#ffffff")); 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
         drawerHeaderView.setBackground(new BitmapDrawable(loadedImage)); 
        } else 
         drawerHeaderView.setBackgroundDrawable(new BitmapDrawable(loadedImage)); 

       } 

       @Override 
       public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
        super.onLoadingFailed(imageUri, view, failReason); 
        drawerHeaderView.setBackgroundResource(R.drawable.app_header); 
        drawerHeaderParent.setBackgroundColor(Color.parseColor((currentTab == 1 ? skinTwo : skin))); 
       } 

       @Override 
       public void onLoadingStarted(String imageUri, View view) { 
        super.onLoadingStarted(imageUri, view); 
        drawerHeaderView.setBackgroundResource(R.drawable.app_header); 
        drawerHeaderParent.setBackgroundColor(Color.parseColor((currentTab == 1 ? skinTwo : skin))); 
       } 
      }); 

      // setting profile pic 
      ImageLoader.getInstance().loadImage(stringBuilder.toString(), displayImageOptions, new SimpleImageLoadingListener() { 
       @Override 
       public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
        super.onLoadingComplete(imageUri, view, loadedImage); 
        drawerProfilePic.setImageBitmap(loadedImage); 
        drawerProfilePic.setVisibility(View.VISIBLE); 
       } 

       @Override 
       public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
        super.onLoadingFailed(imageUri, view, failReason); 
       } 
      }); 


     } else { 
      Toast.makeText(this, getResources().getText(R.string.no_cover_photo), Toast.LENGTH_SHORT).show(); 
      drawerHeaderView.setBackgroundResource(R.drawable.app_header); 
      drawerHeaderParent.setBackgroundColor(Color.parseColor((currentTab == 1 ? skinTwo : skin))); 
     } 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

    Log.d("G+", "Connection suspended"); 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      if (mGoogleApiClient != null) { 

       mGoogleApiClient.connect(); 
      } 
     } 
    }).run(); 
} 

public void onConnectionFailed(final ConnectionResult result) { 
    Log.d("G+", "Connection failed"+ result.getErrorCode()+result.toString()); 

    if (!mIntentInProgress && result.hasResolution()) { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        mIntentInProgress = true; 
        startIntentSenderForResult(result.getResolution().getIntentSender(), 
          RC_SIGN_IN, null, 0, 0, 0); 
       } catch (IntentSender.SendIntentException e) { 
        // The intent was canceled before it was sent. Return to the default 
        // state and attempt to connect to get an updated ConnectionResult. 
        mIntentInProgress = false; 
        if (mGoogleApiClient != null) { 

         mGoogleApiClient.connect(); 
        } 
       } 
      } 
     }).run(); 
    } 
} 

和:

protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 
    if (requestCode == RC_SIGN_IN && !mGoogleApiKey && mGoogleApiClient != null) { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       mIntentInProgress = false; 
       mGoogleApiKey = true; 
       // !mGoogleApiClient.isConnecting 
       if (mGoogleApiClient.isConnecting()) { 
        mGoogleApiClient.connect(); 
       } else 
        mGoogleApiClient.disconnect(); 

      } 
     }).run(); 
    } 

回答

1

你得到了錯誤SIGN_IN_REQUIRED因爲客戶端嘗試連接到該服務但用戶未登錄。

The client may choose to continue without using the API. Alternately, if hasResolution() returns true the client may call [ startResolutionForResult(Activity, int) ](https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity , int)) to prompt the user to sign in. After the sign in activity returns with RESULT_OK further attempts should succeed.

從這個forum基礎,嘗試通過從您的Gmail帳戶要去谷歌API控制檯來解決此問題。您會在Google Drive API前看到禁用按鈕。你會看到一個齒輪或設置按鈕,點擊它並生成oAuth令牌。

這裏有一些相關的主題:

希望這有助於!

+0

我已經生成了auth2令牌,並且我將google_services.json導入到「app /」文件夾中,並且出現此錯誤。是否必須在我的代碼中使用該client_id? – erginduran

+0

btw我檢查了你的鏈接沒有回答的問題,我認爲許多脂肪酶都面臨着這個問題。 – erginduran

0

在我的原因我錯誤地使用發行版SHA1密鑰在開發人員控制檯中生成API。然後用debug.keystore創建SHA1密鑰並在我的API證書中更新。它開始工作。

keytool -list -v -keystore "C:\Users\<user>n\.android\debug.keystore" -alias androiddebugkey -storepass andro 
id -keypass android