2015-12-01 5 views
0

Я получаю ошибку, пытаясь выполнить этот пример API Android Drive googledrive/android-quickstart, приложение работать нормально затем показывает диалог окна, чтобы выбрать учетную запись Google, я выбираю один и LogCat я получаю это:Сбой подключения контура с GoogleApiClient

I/drive-quickstart﹕ GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{44c22a28: [email protected]}} 

Затем показывает диалог снова выглядит как бесконечный цикл

Я уже настроил OAuth и другие параметры в Google консоли разработчика.

Вот мой код Заранее спасибо ..

public class CloudPaintActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 

private static final String TAG = "drive-quickstart"; 
private static final int REQUEST_CODE_CAPTURE_IMAGE = 1; 
private static final int REQUEST_CODE_CREATOR = 2; 
private static final int REQUEST_CODE_RESOLUTION = 3; 

private GoogleApiClient mGoogleApiClient; 
private Bitmap mBitmapToSave; 

/** 
* Create a new file and save it to Drive. 
*/ 
private void saveFileToDrive() { 
    // Start by creating a new contents, and setting a callback. 
    Log.i(TAG, "Creating new contents."); 
    final Bitmap image = mBitmapToSave; 
    Drive.DriveApi.newDriveContents(mGoogleApiClient) 
      .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 

       @Override 
       public void onResult(DriveApi.DriveContentsResult result) { 
        // If the operation was not successful, we cannot do anything 
        // and must 
        // fail. 
        if (!result.getStatus().isSuccess()) { 
         Log.i(TAG, "Failed to create new contents."); 
         return; 
        } 
        // Otherwise, we can write our data to the new contents. 
        Log.i(TAG, "New contents created."); 
        // Get an output stream for the contents. 
        OutputStream outputStream = result.getDriveContents().getOutputStream(); 
        // Write the bitmap data from it. 
        ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream(); 
        image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream); 
        try { 
         outputStream.write(bitmapStream.toByteArray()); 
        } catch (IOException e1) { 
         Log.i(TAG, "Unable to write file contents."); 
        } 
        // Create the initial metadata - MIME type and title. 
        // Note that the user will be able to change the title later. 
        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() 
          .setMimeType("image/jpeg").setTitle("Android Photo.png").build(); 
        // Create an intent for the file chooser, and start it. 
        IntentSender intentSender = Drive.DriveApi 
          .newCreateFileActivityBuilder() 
          .setInitialMetadata(metadataChangeSet) 
          .setInitialDriveContents(result.getDriveContents()) 
          .build(mGoogleApiClient); 
        try { 
         startIntentSenderForResult(
           intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); 
        } catch (IntentSender.SendIntentException e) { 
         Log.i(TAG, "Failed to launch file chooser."); 
        } 
       } 
      }); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    if (mGoogleApiClient == null) { 
     // Create the API client and bind it to an instance variable. 
     // We use this instance as the callback for connection and connection 
     // failures. 
     // Since no account name is passed, the user is prompted to choose. 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(Drive.API) 
       .addApi(Plus.API) 
       .addScope(Drive.SCOPE_FILE) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
    } 
    // Connect the client. Once connected, the camera is launched. 
    mGoogleApiClient.connect(); 
    // if the api client existed, we terminate it 
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { 
     Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); 
     mGoogleApiClient.disconnect(); 
    } 
} 

@Override 
protected void onPause() { 
    if (mGoogleApiClient != null) { 
     mGoogleApiClient.disconnect(); 
    } 
    super.onPause(); 
} 

@Override 
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { 
    switch (requestCode) { 
     case REQUEST_CODE_CAPTURE_IMAGE: 
      // Called after a photo has been taken. 
      if (resultCode == Activity.RESULT_OK) { 
       // Store the image data as a bitmap for writing later. 
       mBitmapToSave = (Bitmap) data.getExtras().get("data"); 
      } 
      break; 
     case REQUEST_CODE_CREATOR: 
      // Called after a file is saved to Drive. 
      if (resultCode == RESULT_OK) { 
       Log.i(TAG, "Image successfully saved."); 
       mBitmapToSave = null; 
       // Just start the camera again for another photo. 
       startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 
         REQUEST_CODE_CAPTURE_IMAGE); 
      } 
      break; 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    // Called whenever the API client fails to connect. 
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString()); 
    if (!result.hasResolution()) { 
     // show the localized error dialog. 
     GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show(); 
     return; 
    } 
    // The failure has a resolution. Resolve it. 
    // Called typically when the app is not yet authorized, and an 
    // authorization 
    // dialog is displayed to the user. 
    try { 
     result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
    } catch (IntentSender.SendIntentException e) { 
     Log.e(TAG, "Exception while starting resolution activity", e); 
    } 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    Log.i(TAG, "API client connected."); 
    if (mBitmapToSave == null) { 
     // This activity has no UI of its own. Just start the camera. 
     startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 
       REQUEST_CODE_CAPTURE_IMAGE); 
     return; 
    } 
    saveFileToDrive(); 
} 

@Override 
public void onConnectionSuspended(int cause) { 
    Log.i(TAG, "GoogleApiClient connection suspended"); 
} 

}

ответ

0

Основы вашей ошибки, кажется, по какой причине он не подпишет. Я считаю, в этом случае вы должны проверить, если hasResolution возвращает true. Если это так, вы можете позвонить по телефону [startResolutionForResult] (https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)), в котором будет предложено войти в систему.

Насколько он не может войти в систему, это немного сложно сказать. Похоже, вы используете AccountPicker? Возможно, вы можете попробовать только одну учетную запись, подписанную на устройство.

2

Попробуйте это, если вам не нужно несколько учетных записей:

public class MainActivity extends AppCompatActivity { 
    private static final int REQ_CONNECT = 1; 

    private Activity mAct; 
    private static GoogleApiClient mGAC; 

    @Override 
    protected void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    mAct = this; 
    setContentView(R.layout.activity_main); 
    if (bundle == null) try { 
     mGAC = new GoogleApiClient.Builder(this) 
     .addApi(Drive.API) 
     .addScope(Drive.SCOPE_FILE) 
     .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
      @Override public void onConnectionSuspended(int i) { } 
      @Override 
      public void onConnected(Bundle bundle) { 
      Toast.makeText(mAct, "bingo", Toast.LENGTH_LONG).show(); // connected 
      saveFileToDrive(); 
      } 
     }) 
     .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
      @Override 
      public void onConnectionFailed(ConnectionResult connResult) { 
      if (connResult != null) { 
       if (!connResult.hasResolution()) { 
       int errCode = connResult.getErrorCode(); 
       GooglePlayServicesUtil.getErrorDialog(errCode, mAct, 0).show(); 
       return; 
       } else try { 
       connResult.startResolutionForResult(mAct, REQ_CONNECT); 
       return; 
       } catch (Exception e) { e.printStackTrace(); } 
      } 
      finish(); //--- FAIL - no resolution ---------->>> 
      } 
     }) 
     .build(); 
    } catch (Exception e) { e.printStackTrace(); } 
    } 

    @Override 
    protected void onResume() { super.onResume(); 
    mGAC.connect(); 
    } 
    @Override 
    protected void onPause() { super.onPause(); 
    mGAC.disconnect(); 
    } 

    @Override 
    protected void onActivityResult(int request, int result, Intent data) { 
    switch (request) { 
     case REQ_CONNECT: 
     if (result == RESULT_OK) 
      mGAC.connect(); 
     else 
      finish(); //--- FAIL, user cancelled ------------->>> 
     break; 
    } 
    super.onActivityResult(request, result, data); 
    } 
} 

Если вам нужно переключить несколько учетных записей, добавьте:

.addApi(Plus.API) 

вашему строителем и звоните:

Plus.AccountApi.clearDefaultAccount(mGAC); 

откуда угодно (меню). Затем создайте новый экземпляр mGAC и подключитесь. Появится диалоговое окно выбора учетной записи. Но ваше приложение не будет знать, какая учетная запись выбрана пользователем (или создана).

Если вам необходимо знать текущий пользователь, вы можете упасть на Plus.API и управлять GooDrive счета себя счета Picker, но вам необходимо реализовать менеджеру аккаунта и экземпляр mGAC с

.setAccountName([ACCOUNT EMAIL]) 

как видно here (следовать REQ_ACCPICK и увидеть Менеджер учетных записей UT.AM).

Good Luck

0

Я наблюдал за поведением вы описываете, когда OAuth клиент 2,0 ID установить на Google Cloud консоли не соответствует APK я пытался бежать, либо подписи-сертификата отпечатка пальца или пакет имя.

Смежные вопросы