gpt4 book ai didi

java - Google 云端硬盘 SDK 异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:31 26 4
gpt4 key购买 nike

我正在尝试运行以下代码(主要取自 Stephen Wylie ):

package com.googledrive.googledriveapp;
// For Google Drive / Play Services
// Version 1.1 - Added new comments & removed dead code
// Stephen Wylie - 10/20/2012
import java.io.IOException;
import java.util.ArrayList;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.android2.AndroidHttp;
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpRequest;
import com.google.api.client.http.json.JsonHttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Apps.List;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.DriveRequest;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;

public class MainActivity extends Activity {
private static final int CHOOSE_ACCOUNT=0;
private static String accountName;
private static int REQUEST_TOKEN=0;
private Button btn_drive;
private Context ctx = this;
private Activity a = this;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
// set up the GUI layout
setContentView(R.layout.activity_main);
// set the variables to access the GUI controls
btn_drive = (Button) findViewById(R.id.btn_drive);
btn_drive.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
chooseAccount();
}
});
}

public void chooseAccount() {
Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null, null, null, null);
startActivityForResult(intent, CHOOSE_ACCOUNT);
}

// Fetch the access token asynchronously.
void getAndUseAuthTokenInAsyncTask(Account account) {
AsyncTask<Account, String, String> task = new AsyncTask<Account, String, String>() {
ProgressDialog progressDlg;
AsyncTask<Account, String, String> me = this;

@Override
protected void onPreExecute() {
progressDlg = new ProgressDialog(ctx, ProgressDialog.STYLE_SPINNER);
progressDlg.setMax(100);
progressDlg.setTitle("Validating...");
progressDlg.setMessage("Verifying the login data you entered...\n\nThis action will time out after 10 seconds.");
progressDlg.setCancelable(false);
progressDlg.setIndeterminate(false);
progressDlg.setOnCancelListener(new android.content.DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface d) {
progressDlg.dismiss();
me.cancel(true);
}
});
progressDlg.show();
}

@Override
protected String doInBackground(Account... params) {
return getAccessToken(params[0]);
}

@Override
protected void onPostExecute(String s) {
if (s == null) {
// Wait for the extra intent
} else {
accountName = s;
getDriveFiles();
}
progressDlg.dismiss();
}
};
task.execute(account);
}

/**
* Fetches the token from a particular Google account chosen by the user. DO NOT RUN THIS DIRECTLY. It must be run asynchronously inside an AsyncTask.
* @param activity
* @param account
* @return
*/
private String getAccessToken(Account account) {
try {
return GoogleAuthUtil.getToken(ctx, account.name, "oauth2:" + DriveScopes.DRIVE); // IMPORTANT: DriveScopes must be changed depending on what level of access you want
} catch (UserRecoverableAuthException e) {
// Start the Approval Screen intent, if not run from an Activity, add the Intent.FLAG_ACTIVITY_NEW_TASK flag.
a.startActivityForResult(e.getIntent(), REQUEST_TOKEN);
e.printStackTrace();
return null;
} catch (GoogleAuthException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

private Drive getDriveService() {
HttpTransport ht = AndroidHttp.newCompatibleTransport(); // Makes a transport compatible with both Android 2.2- and 2.3+
JacksonFactory jf = new JacksonFactory(); // You need a JSON parser to help you out with the API response
Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accountName);
HttpRequestFactory rf = ht.createRequestFactory(credential);
Drive.Builder b = new Drive.Builder(ht, jf, null);
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {

@Override
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest.setOauthToken(accountName);
}
});
return b.build();
}

/**
* Obtains a list of all files on the signed-in user's Google Drive account.
*/
private void getDriveFiles() {
Drive service = getDriveService();
Log.d("SiteTrack", "FUNCTION getDriveFiles()");
Files.List request;
try {
request = service.files().list(); // .setQ("mimeType=\"text/plain\"");
} catch (IOException e) {


e.printStackTrace();
return;
}
do {
FileList files;
try {
System.out.println("got here");
Log.d("SiteTrack", request.toString());
**files = request.execute();**
} catch (IOException e) {
e.printStackTrace();
Log.d("SiteTrack", "Exception");
return;
}
ArrayList<File> fileList = (ArrayList<File>) files.getItems();
Log.d("SiteTrack", "Files found: " + files.getItems().size());
for (File f : fileList) {
String fileId = f.getId();
String title = f.getTitle();
Log.d("SiteTrack", "File " + fileId + ": " + title);
}
request.setPageToken(files.getNextPageToken());
} while (request.getPageToken() != null && request.getPageToken().length() >= 0);
}

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (requestCode == CHOOSE_ACCOUNT && resultCode == RESULT_OK) {
accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
GoogleAccountManager gam = new GoogleAccountManager(this);
getAndUseAuthTokenInAsyncTask(gam.getAccountByName(accountName));
Log.d("SiteTrack", "CHOOSE_ACCOUNT");
} else if (requestCode == REQUEST_TOKEN && resultCode == RESULT_OK) {
accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
Log.d("SiteTrack", "REQUEST_TOKEN");
}
}
}

但是,我得到以下异常:

11-19 16:35:27.758: W/System.err(23287): com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
11-19 16:35:27.758: W/System.err(23287): {
11-19 16:35:27.758: W/System.err(23287): "code" : 403,
11-19 16:35:27.758: W/System.err(23287): "errors" : [ {
11-19 16:35:27.758: W/System.err(23287): "domain" : "usageLimits",
11-19 16:35:27.762: W/System.err(23287): "message" : "Access Not Configured",
11-19 16:35:27.762: W/System.err(23287): "reason" : "accessNotConfigured"
11-19 16:35:27.762: W/System.err(23287): } ],
11-19 16:35:27.762: W/System.err(23287): "message" : "Access Not Configured"
11-19 16:35:27.762: W/System.err(23287): }
11-19 16:35:27.762: W/System.err(23287): at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:237)
11-19 16:35:27.762: W/System.err(23287): at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:207)
11-19 16:35:27.762: W/System.err(23287): at com.google.api.services.drive.Drive$Files$List.execute(Drive.java:1071)
11-19 16:35:27.762: W/System.err(23287): at com.googledrive.googledriveapp.MainActivity.getDriveFiles(MainActivity.java:173)
11-19 16:35:27.762: W/System.err(23287): at com.googledrive.googledriveapp.MainActivity.access$3(MainActivity.java:156)
11-19 16:35:27.762: W/System.err(23287): at com.googledrive.googledriveapp.MainActivity$2.onPostExecute(MainActivity.java:104)
11-19 16:35:27.765: W/System.err(23287): at com.googledrive.googledriveapp.MainActivity$2.onPostExecute(MainActivity.java:1)
11-19 16:35:27.765: W/System.err(23287): at android.os.AsyncTask.finish(AsyncTask.java:417)
11-19 16:35:27.765: W/System.err(23287): at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-19 16:35:27.765: W/System.err(23287): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-19 16:35:27.765: W/System.err(23287): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 16:35:27.765: W/System.err(23287): at android.os.Looper.loop(Looper.java:123)
11-19 16:35:27.765: W/System.err(23287): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-19 16:35:27.765: W/System.err(23287): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 16:35:27.769: W/System.err(23287): at java.lang.reflect.Method.invoke(Method.java:521)
11-19 16:35:27.769: W/System.err(23287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-19 16:35:27.769: W/System.err(23287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-19 16:35:27.769: W/System.err(23287): at dalvik.system.NativeStart.main(Native Method)

追踪到我上面代码中的 files = request.execute(); 行(我用星号标记了它)。我在我的 Google API 控制台中同时启用了 Drive SDK 和 Drive API。以下是我的 Drive SDK 设置的几个快照: enter image description here enter image description here对于未显示的客户端 ID 部分,我只是从 API 访问部分粘贴了“已安装应用程序的客户端 ID”(我还尝试了“Drive SDK 的客户端 ID”)。有人知道问题出在哪里吗?

最佳答案

已编辑:抱歉,我刚刚意识到我们可以使用 debug.keystore 在 Debug模式下对 apk 进行签名。因此,最重要的是在您的 Google API 控制台中插入正确的 SHA1 key 。 (使用 Stephen Wylie 在他的 Google+ 帖子中提到的 JRE7 工具生成)

另请参阅 (developer.android.com/tools/publishing/app-signing.html#debugmode) 以获取 keystore 密码

经过反复试验,我终于能够列出我的 google 驱动器中的文件虽然我不确定这是否是最正确的答案,但至少我现在可以列出文件了

首先,请引用 Stephen Wylie 更新的 Google+ 帖子 https://plus.google.com/u/0/114042449736049687152/posts/CD3L8zcJg5Z

使用 JRE7 工具通过您的 .keystore 生成 SHA1 key

我正在使用我在我的谷歌游戏商店中使用的 .keystore 文件(如果你没有任何keystore,你可以去你的项目,点击export,然后它会要求你创建一个.keystore文件)

使用生成的 SHA1 key ,转到您的 Google API 控制台(最好是从新开始,删除当前项目并开始一个新项目)

-启用Drive API

-启用Drive SDK

-转到API访问

-创建客户端ID

-此处重要,选择已安装的应用程序和 Android

-另外重要,输入您生成的 SHA1 key

-键入您在 eclipse 项目中使用的正确包名称 (com.example.xxx)

-转到 Drive SDK 选项卡-上传图标

-重要,在 API 访问选项卡中输入您的客户端 ID(已安装应用程序的客户端 ID)

-插入 Stephen Wylie 帖子中提到的 3 个范围,[/userinfo.email,/userinfo.profile,/auth/drive]

-插入网址

-勾选多文件支持

再次确保代码中的包名与插入 Google API 控制台的包名相同

最后,在 eclipse 中,使用您刚才创建的 .keystore 文件导出您的项目

将导出的APK文件放入手机,安装试试。

检查您的 LogCat 以显示您的 Google Drive 中列出的文件

我用这种方式成功了。

已编辑:如果您使用 debug.keystore 生成了 SHA1 key ,则可以跳过“导出”部分。只需调试您的应用程序即可。 Eclipse 将自动使用 debug.keystore 对 apk 进行签名。

已编辑:下次您的代码准备就绪时,您需要使用真实的 .keystore 生成一个新的 SHA1 key ,然后输入到 Google API 控制台。

编辑 2:确保您的 list 包含这些

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

jars that I used Imports

关于java - Google 云端硬盘 SDK 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13462952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com