使用Google Drive API for Android打开文件INTERNAL_ERROR

时间:2015-08-19 21:32:29

标签: android google-drive-api google-drive-android-api

我正在尝试使用Google Drive API for Android打开文件。从以下官方tutorial,我有以下内容:

GoogleDriveActivity.class

public class GoogleDriveActivity extends Activity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {

private GoogleApiClient mGoogleApiClient;
private int REQUEST_CODE_RESOLUTION = 1;
private int REQUEST_CODE_OPENER = 2;
private ListView filesLv;
private DataBufferAdapter<Metadata> mResultsAdapter;
private String mNextPageToken;
private boolean hasMore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_drive);

    filesLv = (ListView) findViewById(R.id.listViewResults);
    hasMore = true;
    mResultsAdapter = new ResultsAdapter(this);
    filesLv.setAdapter(mResultsAdapter);
    filesLv.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int first, int visible, int total) {
            if (mNextPageToken != null && first + visible + 5 < total) {
                retrieveNextPage();
            }
        }
    });
}

private void retrieveNextPage() {
    // retrieve the results for the next page.
    Query query = new Query.Builder()
            .setPageToken(mNextPageToken)
            .build();
    Drive.DriveApi.query(mGoogleApiClient, query)
            .setResultCallback(metadataBufferCallback);
}

private final ResultCallback<DriveApi.MetadataBufferResult> metadataBufferCallback = new
        ResultCallback<DriveApi.MetadataBufferResult>() {
            @Override
            public void onResult(DriveApi.MetadataBufferResult result) {
                if (!result.getStatus().isSuccess()) {
                    return;
                }
                mResultsAdapter.clear();
                mResultsAdapter.append(result.getMetadataBuffer());
                mNextPageToken = result.getMetadataBuffer().getNextPageToken();
                hasMore = mNextPageToken != null;
            }
        };

@Override
public void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .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 onConnectionFailed(ConnectionResult connectionResult) {
    if (!connectionResult.hasResolution()) {
        return;
    }
    try {
        connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (IntentSender.SendIntentException e) {
    }

}

@Override
public void onConnected(Bundle bundle) {
    retrieveNextPage();
}

@Override
public void onConnectionSuspended(int i) {
}
}

ResultsAdapter.class:

public class ResultsAdapter extends DataBufferAdapter<Metadata> {

public ResultsAdapter(Context context) {
    super(context, android.R.layout.simple_list_item_1);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = View.inflate(getContext(),
                android.R.layout.simple_list_item_1, null);
    }
    Metadata metadata = getItem(position);
    TextView titleTextView =
            (TextView) convertView.findViewById(android.R.id.text1);
    titleTextView.setText(metadata.getTitle());
    return convertView;
}
}

我将依赖项包含在Gradle文件中,如下所示:

compile 'com.google.android.gms:play-services-drive:7.8.0'

Activity中的Manifest.xml如下所示:

<activity
            android:name="com.myproject.GoogleDriveActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan">
            <meta-data android:name="com.google.android.apps.drive.APP_ID" android:value="id=<google project number>"/>
</activity>

请注意,我已使用包名称将SHA1添加到Google API吊顶。此外,内容屏幕中的字段按照here说明填写。

当我尝试运行此代码时,我在onConnectionFailed回调中不断收到以下错误消息:

{statusCode=INTERNAL_ERROR, resolution=null}

关于可能出现什么问题的任何想法?我无法弄清问题是什么。

1 个答案:

答案 0 :(得分:0)

我找到了答案。问题是debug key。基本上,我运行keytool命令并生成SHA1,然后我将其添加到Google上的API控制台。然后我从Android Studio运行该项目。这给了我错误。

然后我从Android Studio创建了一个新的密钥库 - &gt;菜单 - &gt;构建 - &gt;生成签名的apk。执行相同的命令以生成我上传到API控制台的SHA1。然后使用更新的apk文件,我可以获取文件的内容。

问题是Google无法对密钥进行身份验证。