【问题标题】:Open file using Google Drive API's for Android | INTERNAL_ERROR使用适用于 Android 的 Google Drive API 打开文件 |内部错误
【发布时间】:2015-08-19 21:32:28
【问题描述】:

我正在尝试使用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'

Manifest.xml 中的Activity 如下所示:

<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 condole。此外,内容屏幕中的字段按照here 的说明填写。

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

{statusCode=INTERNAL_ERROR, resolution=null}

对可能出现的问题有任何想法吗?我无法弄清楚问题是什么。

【问题讨论】:

  • 只是为了澄清一下,您到底在哪里得到了错误?听起来你的 GoogleApiClient 没有被连接,你能确认 onConnected 方法被调用了吗?
  • @kroikie 感谢您指出这一点。我编辑了我的问题。我在onConnectionFailed 回调中遇到错误。它根本没有连接。
  • 您能否确认您的清单文件中的包名称与您的 gradle 文件中的 applicationId 以及开发控制台中客户端 ID 中的包名称相同?
  • 嗨@kroikie。我发现了这个问题并将其添加为问题的答案。感谢您查看这个。欣赏它。

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


【解决方案1】:

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

然后我从 Android Studio -> Menu -> Build -> Generate signed apk 创建了一个新的密钥库。运行相同的命令来生成我上传到 API 控制台的SHA1。然后使用更新的apk 文件,我能够获取文件的内容。

问题在于 Google 无法验证密钥。

【讨论】:

  • 您刚刚遇到了一个经典的身份验证陷阱。描述了此问题的最后一搏/双重检查here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多