简体   繁体   English

如何使用Google Drive API密钥从android应用程序访问驱动器内容?

[英]How do I use the Google drive API key to access drive content from android application?

I can't access Google drive. 我无法访问Google云端硬盘。 I created an API KEY in Google API console and enabled Google Drive API and SDK. 我在Google API控制台中创建了API KEY,并启用了Google Drive API和SDK。 Where do I set that API KEY in code? 如何在代码中设置该API密钥?

Drive builder does not have a setJsonHttpRequestInitializer method. 驱动器构建器没有setJsonHttpRequestInitializer方法。 Where can I set the KEY?Is there alternatives? 我在哪里可以设定KEY?

private Drive getDriveService(String token) {

HttpTransport ht = AndroidHttp.newCompatibleTransport(); 
JacksonFactory jsonFactory = new JacksonFactory();          
Credential credentials = new GoogleCredential().setAccessToken(token);                                                          

    Drive.Builder b = new Drive.Builder(ht, jsonFactory, credentials);
    b.setHttpRequestInitializer(credentials);

return b.build();
}

Getting: 获得:

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

You need to activate TWO things in the API console: - Drive API - Drive SDK 您需要在API控制台中激活两件事:-驱动器API-驱动器SDK

Many people activate only one. 许多人只能激活一个。

Moreover, don't forget to add your real certificate SHA1 before releasing the final application. 此外,不要忘记在发布最终应用程序之前添加您的真实证书SHA1。

There is this similar answer: Google Drive SDK Exception 有类似的答案: Google Drive SDK异常

In Android, as specified by pommedeterresautee we need to perform the activation of Both Drive API and Drive SDK, which then leads to the API Key. 在Android中,按照pommedeterresautee的指定,我们需要执行Drive API和Drive SDK的激活,然后激活API Key。

This has to be set in the Android manifest inside the application tag and on par with other application component tags as : 这必须在Android清单中的application标签内进行设置,并且与其他application组件标签相同,如下所示:

<application...>
<activity ...>
<meta-data
        android:name="com.google.android.apps.drive.APP_ID"
        android:value="id=YOUR_API_KEY" />
</application>

When you do this, you will be able to access the api without a 403 "Access Not Configured" problem. 执行此操作时,您将能够访问api,而不会出现403“未配置访问权限”问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM