Skip to content

Instantly share code, notes, and snippets.

@MobiDevelop
Last active June 22, 2017 04:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MobiDevelop/7682501 to your computer and use it in GitHub Desktop.
Save MobiDevelop/7682501 to your computer and use it in GitHub Desktop.
Google Drive Problem
Additional Info
---------------
Nexus 5
Android 4.4
Using either Android Studio or Eclipse results in the same exception
With Eclipse, added Drive API v2 from the Google Plugin. With Android Studio, included the jars manually.
Added Google Play Services v13 (com.google.android.gms:play-services:4.0.+ in build.gradle).
The code is pretty much an exact copy from the Google Drive Quickstart (changed package name, and fixed a method signature).
---
Note - I changed the package name from the real app and removed my account it from the stack trace.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.drive"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.me.drive.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
package com.me.drive;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.http.FileContent;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
public class MainActivity extends Activity {
static final int REQUEST_ACCOUNT_PICKER = 1;
static final int REQUEST_AUTHORIZATION = 2;
static final int CAPTURE_IMAGE = 3;
private static Uri fileUri;
private static Drive service;
private GoogleAccountCredential credential;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE));
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_ACCOUNT_PICKER:
if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
startCameraIntent();
}
}
break;
case REQUEST_AUTHORIZATION:
if (resultCode == Activity.RESULT_OK) {
saveFileToDrive();
} else {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
break;
case CAPTURE_IMAGE:
if (resultCode == Activity.RESULT_OK) {
saveFileToDrive();
}
}
}
private void startCameraIntent() {
String mediaStorageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator + "IMG_"
+ timeStamp + ".jpg"));
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, CAPTURE_IMAGE);
}
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
if (file != null) {
showToast("Photo uploaded: " + file.getTitle());
startCameraIntent();
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (GoogleAuthIOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
}
public void showToast(final String toast) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
}
});
}
}
11-27 12:34:36.890: W/GLSActivity(6026): [aia] Status from wire: status: null
11-27 12:34:36.890: W/GLSActivity(6026): [aia] Status from wire: status: null
11-27 12:34:36.890: I/GLSUser(6026): GLS error: <<ACCOUNT_EMAIL>> oauth2: https://www.googleapis.com/auth/drive
11-27 12:34:36.890: W/GLSActivity(6026): [aia] Status from wire: Unknown status: UNKNOWN
11-27 12:34:36.890: W/System.err(3436): com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:286)
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858)
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:559)
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithBackOffAndGZip(MediaHttpUploader.java:581)
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:533)
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:393)
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:345)
11-27 12:34:36.890: W/System.err(3436): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:418)
11-27 12:34:36.900: W/System.err(3436): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
11-27 12:34:36.900: W/System.err(3436): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
11-27 12:34:36.900: W/System.err(3436): at com.me.drive.MainActivity$1.run(MainActivity.java:97)
11-27 12:34:36.900: W/System.err(3436): at java.lang.Thread.run(Thread.java:841)
11-27 12:34:36.900: W/System.err(3436): Caused by: com.google.android.gms.auth.GoogleAuthException: Unknown
11-27 12:34:36.900: W/System.err(3436): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
11-27 12:34:36.900: W/System.err(3436): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
11-27 12:34:36.900: W/System.err(3436): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
11-27 12:34:36.900: W/System.err(3436): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
11-27 12:34:36.900: W/System.err(3436): ... 11 more
@chandlernicole
Copy link

Did you ever figure out what was wrong?? I'm experiencing the same issue..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment