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

2024-04-02

我正在创建一个在 Android 上使用 Google Drive 的应用程序。

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

Object Drive.Builder does not have method setJsonHttpRequestInitializer.

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

哪里有问题?

Source:

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 密钥

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 http://www.sendspace.com/file/an6xxy

出了什么问题,也许是证书指纹 (SHA1) 出了问题?

尝试来自谷歌的calendar-android-sample。

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

我在样本中获得的客户 ID 应该放在哪里?

在 android 中以调试模式运行示例后,出现“访问未配置”。


在 Android 上,最佳实践是使用 Google Play 服务进行 OAuth 2.0。从该库的 1.12.0-beta 版本开始,您应该使用Google帐户凭据 http://javadoc.google-api-java-client.googlecode.com/hg/1.12.0-beta/com/google/api/client/googleapis/extensions/android/gms/auth/GoogleAccountCredential.html来实现这一点。

请使用我写的例子日历 Android 示例 http://samples.google-api-java-client.googlecode.com/hg/calendar-android-sample/instructions.html作为 Android 上 OAuth 2.0 最佳实践的指南。请仔细阅读说明。请注意,您必须首先在Google API 控制台 https://code.google.com/apis/console/为了这个工作。示例中的代码片段:

@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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Drive.Builder Android 类型的 setJsonHttpRequestInitializer 方法未定义 的相关文章

随机推荐