gpt4 book ai didi

android - 方法 setJsonHttpRequestInitializer 未定义类型 Drive.Builder Android

转载 作者:太空狗 更新时间:2023-10-29 12:49:51 24 4
gpt4 key购买 nike

我正在创建一个应用程序以在 Android 上使用 Google 云端硬盘。

我在互联网上搜索了几个例子,但它们都是一样的。我收到以下错误:

Object Drive.Builder does not have method setJsonHttpRequestInitializer.

我已经使用 Google Drive API V1 和 V2 进行了测试,但没有成功。

问题出在哪里?

来源:

private Drive getDriveService(String accountName) {

HttpTransport ht = AndroidHttp.newCompatibleTransport();
JacksonFactory jf = new JacksonFactory();
Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accountName);

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);
driveRequest.setKey(API_KEY);
}
});
return b.build();
}

使用google-api-java-client-1.12.0-beta发现setJsonHttpRequestInitializer方法未定义

下载并导入 google-api-java-client-1.11.0-beta

但现在得到:将客户端 ID 用于已安装的应用程序:客户端 ID。

com.google.api.client.http.HttpResponseException: 403 Forbidden
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "accessNotConfigured",
"message": "Access Not Configured"
}
],
"code": 403,
"message": "Access Not Configured"
}
}

使用简单的 API 访问:API key

Exceptioncom.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}

这里是项目源:

http://www.sendspace.com/file/an6xxy

有什么问题,可能是证书指纹 (SHA1) 的问题?

尝试使用 google 的 calendar-android-sample。

看起来我在 google api 控制台中注册应用程序时遇到了问题。

我在样本中获取的客户 ID 放在哪里?

在 android 中以 Debug模式运行示例后,得到“访问未配置”。

最佳答案

在 Android 上,最佳做法是使用适用于 OAuth 2.0 的 Google Play 服务。从库的 1.12.0-beta 版开始,您应该使用 GoogleAccountCredential来完成这个。

请用我写的例子calendar-android-sample作为 Android 上 OAuth 2.0 最佳实践的指南。请仔细阅读说明。请注意,您必须先在 Google APIs Console 中注册您的应用程序为此工作。样本中的代码 fragment :

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Google Accounts
credential = GoogleAccountCredential.usingOAuth2(this, CalendarScopes.CALENDAR);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
// Calendar client
client = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential).setApplicationName("Google-CalendarAndroidSample/1.0")
.build();
}

关于android - 方法 setJsonHttpRequestInitializer 未定义类型 Drive.Builder Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13398607/

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