我在developers.google.com中关注Drive v2的所有信息,以便在Android API 15中构建一个简单的应用程序,只是为了上传一个txt文件。在它周围花了好几个小时就像库的正确的java构建路径(在我的情况下是1.11.0),启用并设置Drive SDK,客户端ID的API访问等等。我在这篇文章中找到了一个很好的例子Google Drive Returning Error 400 or 403 to my Android App?我找到了相同的错误说明,现在我正在使用我的应用。这是我的完整代码(希望对其他人有用):
public class DrivexampleActivity extends Activity {
Context activity = null;
boolean alreadyTriedAgain;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AccountManager am = AccountManager.get(this);
activity = this.getApplicationContext();
Bundle options = new Bundle();
Account[] acc = am.getAccounts();
am.getAuthToken(
(am.getAccounts())[8], // #8 just in my phone case. you can debugg the acc variable to find your @gmail account index.
"oauth2:" + DriveScopes.DRIVE,
options,
true,
new OnTokenAcquired(),
null);
}
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
@Override
public void run(AccountManagerFuture<Bundle> result) {
try {
final String token = result.getResult().getString(AccountManager.KEY_AUTHTOKEN);
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) throws IOException {
// TODO Auto-generated method stub
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest.setKey("xxxxxxxxxxxx.apps.googleusercontent.com"); // I replaced the number with x's. for your Client ID from Google API Console
driveRequest.setOauthToken(token);
}
});
final Drive drive = b.build();
final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle("My Test File");
body.setDescription("A Test File");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("document.txt");
final FileContent mediaContent = new FileContent("text/plain",fileContent);
new Thread(new Runnable() {
public void run() {
com.google.api.services.drive.model.File file;
try {
file = drive.files().insert(body, mediaContent).execute();
Log.i("Hi", "File ID: " + file.getId());
alreadyTriedAgain = false; // Global boolean to make sure you don't repeatedly try too many times when the server is down or your code is faulty... they'll block requests until the next day if you make 10 bad requests, I found.
} catch (IOException e) {
if(!alreadyTriedAgain){
alreadyTriedAgain = true;
Log.i("Hi", "The upload/insert was caught, which suggests it wasn't successful...");
e.printStackTrace();
}
}
}
}).start();
Intent launch = null;
launch = (Intent)result.getResult().get(AccountManager.KEY_INTENT);
if (launch != null) {
Log.i("Hi", "Something came back as a KEY_INTENT");
startActivityForResult(launch, 3025);
return;
}
} catch (OperationCanceledException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (AuthenticatorException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 3025) {
switch (resultCode) {
case RESULT_OK:
AccountManager am = AccountManager.get(activity);
Bundle options = new Bundle();
am.getAuthToken(
(am.getAccounts())[8], // #8 just in my phone case. you can debugg the acc variable to find your @gmail account index.
"oauth2:" + DriveScopes.DRIVE,
options,
true,
new OnTokenAcquired(),
null);
break;
case RESULT_CANCELED:
// This probably means the user refused to log in. Explain to them why they need to log in.
break;
default:
// This isn't expected... maybe just log whatever code was returned.
break;
}
} else {
// Your application has other intents that it fires off besides the one for Drive's log in if it ever reaches this spot. Handle it here however you'd like.
}
}
同样表现出来:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drivexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".DrivexampleActivity"
android:label="@string/app_name"
android:exported="true">
<meta-data android:name="com.google.android.apps.drive.APP_ID" android:value="id=xxxxxxxxxxxx"/>
<intent-filter>
<action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
<data android:mimeType="application/vnd.test.type"/>
</intent-filter>
</activity>
</application>
</manifest>
但总是得到以下错误:
com.google.api.client.http.HttpResponseException: 400 Bad Request
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "keyInvalid",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
在我的情况下,似乎DRIVE SDK和Drive API没有错误。有什么建议??请!!
答案 0 :(得分:1)
您将从API控制台获取的客户端ID传递给driveRequest.setKey,但该方法用于设置API密钥而不是客户端ID。查看您提到的其他问题的第一个答案,了解更多详情。