Google 驱动器文件:未找到项目或您无权访问它
Google Drive file: Item not found or you are not authorized to access it
我对 Google API 非常陌生:我已经阅读了文档,我的应用程序 [来自 android-demos-master] 成功创建了一个文件,并且内容是 "Hello World"。尝试检索内容时,应用程序连接成功并返回 DriveID。但是,状态代码 1502 "item not found or you do not have authorization" 已重新调整。
我做错了什么?
Manifest [虽然 Activity Intent 过滤器没有完成任何功能]:
<?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>
<action android:name="android.intent.action.MAIN" />
<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>
<action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
</intent-filter>
</activity>
</application>
导致问题的 RetrieveContentsActivity:
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
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 它们都扩展了
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("Created a file with content: " + result.getDriveFile().getDriveId());
然后在读取文件时使用常量:
Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
.setResultCallback(idCallback);
确保 EXISTING_FILE_ID 是您在创建文件时获得的值。也许将 result.getDriveFile().getDriveId() 写入日志会更好,而不是在屏幕上显示几秒钟。
替换:
showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
与:
Log.d("DriveID",Created a file with content: " + result.getDriveFile().getDriveId());
然后记下驱动器 ID,并更新常量:EXISTING_FILE_ID。
我对 Google API 非常陌生:我已经阅读了文档,我的应用程序 [来自 android-demos-master] 成功创建了一个文件,并且内容是 "Hello World"。尝试检索内容时,应用程序连接成功并返回 DriveID。但是,状态代码 1502 "item not found or you do not have authorization" 已重新调整。
我做错了什么?
Manifest [虽然 Activity Intent 过滤器没有完成任何功能]:
<?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>
<action android:name="android.intent.action.MAIN" />
<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>
<action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
</intent-filter>
</activity>
</application>
导致问题的 RetrieveContentsActivity:
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
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 它们都扩展了
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("Created a file with content: " + result.getDriveFile().getDriveId());
然后在读取文件时使用常量:
Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
.setResultCallback(idCallback);
确保 EXISTING_FILE_ID 是您在创建文件时获得的值。也许将 result.getDriveFile().getDriveId() 写入日志会更好,而不是在屏幕上显示几秒钟。 替换:
showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
与:
Log.d("DriveID",Created a file with content: " + result.getDriveFile().getDriveId());
然后记下驱动器 ID,并更新常量:EXISTING_FILE_ID。