Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Google Drive在我的Android应用程序中返回错误400_Java_Android_Xml_Google Drive Api - Fatal编程技术网

Java Google Drive在我的Android应用程序中返回错误400

Java Google Drive在我的Android应用程序中返回错误400,java,android,xml,google-drive-api,Java,Android,Xml,Google Drive Api,为了在Android API 15中构建一个简单的应用程序,只需上传一个txt文件,我在developers.google.com上发布了关于Drive v2的所有信息。在花了很多时间研究它之后,比如库的正确java构建路径(在我的例子中是1.11.0),启用并设置驱动器SDK,客户端ID的API访问等等。我在这篇文章中找到了一个很好的例子,在这里我发现了与我现在在我的应用程序上看到的相同的错误描述。这是我的完整代码(希望对其他人有用): 在我的例子中,驱动器SDK和驱动器API似乎没有错误。有

为了在Android API 15中构建一个简单的应用程序,只需上传一个txt文件,我在developers.google.com上发布了关于Drive v2的所有信息。在花了很多时间研究它之后,比如库的正确java构建路径(在我的例子中是1.11.0),启用并设置驱动器SDK,客户端ID的API访问等等。我在这篇文章中找到了一个很好的例子,在这里我发现了与我现在在我的应用程序上看到的相同的错误描述。这是我的完整代码(希望对其他人有用):


在我的例子中,驱动器SDK和驱动器API似乎没有错误。有什么建议吗??求你了

您正在将从API控制台获得的客户端ID传递给driveRequest.setKey,但是该方法用于设置API密钥而不是客户端ID。有关详细信息,请查看您提到的其他问题的第一个答案。

您正在将从API控制台获得的客户端ID传递给driveRequest.setKey,但是,该方法用于设置API密钥,而不是客户端ID。有关详细信息,请查看您提到的其他问题的第一个答案。

您说得对!,使用SimpleAPI访问下提供的API密钥,我获得了访问权限。现在我得到了一个“java.io.FileNotFoundException:/document.txt:open failed:enoint(没有这样的文件或目录)”我以为document.txt文件是以前由filecontent创建的??filecontent用于读取计算机上现有文件的内容所以,它是由java.io.file filecontent=new java.io.file(“document.txt”)创建的;但是放在哪里呢?。我认为这个示例正在上载一个程序创建的文件。不,该文件必须手动创建。阅读《已解决问题》中的步骤1!由于应用程序在调试模式下在我的设备中运行,因此不允许/装载SD卡来放置上载文件。谢谢!你是对的!,使用SimpleAPI访问下提供的API密钥,我获得了访问权限。现在我得到了一个“java.io.FileNotFoundException:/document.txt:open failed:enoint(没有这样的文件或目录)”我以为document.txt文件是以前由filecontent创建的??filecontent用于读取计算机上现有文件的内容所以,它是由java.io.file filecontent=new java.io.file(“document.txt”)创建的;但是放在哪里呢?。我认为这个示例正在上载一个程序创建的文件。不,该文件必须手动创建。阅读《已解决问题》中的步骤1!由于应用程序在调试模式下在我的设备中运行,因此不允许/装载SD卡来放置上载文件。谢谢!
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"
 }
}