关于 android:Google Drive 文件:找不到项目或您无权访问它

Google Drive file: Item not found or you are not authorized to access it

我对 Google API 非常陌生:我浏览了文档,我的应用 [来自 android-demos-master] 成功创建了一个文件,内容是"Hello World"。尝试检索内容时,应用程序连接成功并返回 DriveID。但是,状态码 1502"找不到项目或您没有授权"被重新调整。

我做错了什么?

Manifest [虽然 Activity Intent 过滤器不满足任何功能]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dishes4"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="19" />

<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">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
           

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".CreateFileActivity"
        android:label="@string/title_activity_create_file">



    </activity>
    <activity
        android:name=".BaseDemoActivity"
        android:label="@string/title_activity_base_demo">
    </activity>
    <activity
        android:name=".RetrieveContentsActivity"
        android:label="@string/title_activity_retrieve_contents">

         <meta-data android:name="com.google.android.apps.drive.APP_ID" android:value="id=108614704480-ohna61c6boi21ak393pqhvjh1dm1or6f.apps.googleusercontent.com" />
    <intent-filter>
   

    </intent-filter>
    </activity>
</application>

导致问题的 RetrieveContentsActivity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.example.dishes4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;
    import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveId;


public class RetrieveContentsActivity extends BaseDemoActivity {

private static final String TAG ="RetrieveContentsActivity";
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retrieve_contents);
    tv = (TextView) findViewById(R.id.text);
}

@Override
public void onConnected(Bundle connectionHint) {
    tv.append("Connected");
    showMessage("Connected");
    super.onConnected(connectionHint);
    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
            .setResultCallback(idCallback);
    tv.append("........   finising fetching DriveID");

}


final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
    @Override
    public void onResult(DriveIdResult result) {
        tv.append("Got a result!!!!!!!!!!!     ");
        showMessage("Got a result!!!!!!!!!!!     ");
        if (result.getStatus().isSuccess()) {
            tv.append("Success  DriveIDResult");

        tv.append(result.toString());
        DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), result.getDriveId());
            return;
        }

        else
        {tv.append("Status code: "+Integer.toString(result.getStatus().getStatusCode())+"    ");
        showMessage("Cannot find DriveId. Are you authorized to view this file?");
        return;}
    }
};
}

CreateFileActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  package com.example.dishes4;

import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveFolder.DriveFileResult;
import com.google.android.gms.drive.MetadataChangeSet;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class CreateFileActivity extends BaseDemoActivity {

private static final String TAG ="CreateFileActivity";

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    // create new contents resource
    Drive.DriveApi.newDriveContents(getGoogleApiClient())
            .setResultCallback(driveContentsCallback);
}

final private ResultCallback<DriveContentsResult> driveContentsCallback = new
        ResultCallback<DriveContentsResult>() {
    @Override
    public void onResult(DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }
        final DriveContents driveContents = result.getDriveContents();

        // Perform I/O off the UI thread.
        new Thread() {
            @Override
            public void run() {
                // write content to DriveContents
                OutputStream outputStream = driveContents.getOutputStream();
                Writer writer = new OutputStreamWriter(outputStream);
                try {
                    writer.write("Hello World!");
                    writer.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage());
                }

                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setTitle("New file")
                        .setMimeType("text/plain")
                        .setStarred(true).build();

                // create a file on root folder
                Drive.DriveApi.getRootFolder(getGoogleApiClient())
                        .createFile(getGoogleApiClient(), changeSet, driveContents)
                        .setResultCallback(fileCallback);
            }
        }.start();
    }
};

final private ResultCallback<DriveFileResult> fileCallback = new
        ResultCallback<DriveFileResult>() {
    @Override
    public void onResult(DriveFileResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create the file");
            return;
        }
        showMessage("Created a file with content:" + result.getDriveFile().getDriveId());
    }
};
}

和 BaseDemoActivity 它们都扩展了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.example.dishes4;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;


public abstract class BaseDemoActivity extends Activity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final String TAG ="BaseDriveActivity";
public static final String EXISTING_FILE_ID ="0ByfSjdPVs9MZTHBmMVdSeWxaNTg";
protected static final String EXTRA_ACCOUNT_NAME ="account_name";
protected static final int REQUEST_CODE_RESOLUTION = 1;
protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;
private GoogleApiClient mGoogleApiClient;


@Override
protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    mGoogleApiClient.connect();
}


@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
        mGoogleApiClient.connect();
    }
}

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

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG,"GoogleApiClient connected");
}

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

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG,"GoogleApiClient connection failed:" + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
        return;
    }
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (SendIntentException e) {
        Log.e(TAG,"Exception while starting resolution activity", e);
    }
}

public void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

public GoogleApiClient getGoogleApiClient() {
  return mGoogleApiClient;
  }
}


由于没有人回答您的问题。我冒昧地回答,即使它已经超过几个星期了。

当您创建文件时,您会使用以下几行显示您的驱动器 ID:
showMessage("创建了一个包含内容的文件:" result.getDriveFile().getDriveId());

那么当你读取你的文件时,你使用的是一个常量:

1
2
Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
        .setResultCallback(idCallback);

确保 EXISTING_FILE_ID 是您在创建文件时获得的值。也许将 result.getDriveFile().getDriveId() 写入日志会更好,而不是在屏幕上显示几秒钟。
替换:

1
showMessage("Created a file with content:" + result.getDriveFile().getDriveId());

与:

1
Log.d("DriveID",Created a file with content:" + result.getDriveFile().getDriveId());

然后记下驱动器 ID,并更新常量:EXISTING_FILE_ID。