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

use*_*780 2 android google-drive-api google-drive-realtime-api

我无法访问Google云端硬盘.我在Google API控制台中创建了一个API KEY,并启用了Google Drive API和SDK.我在哪里在代码中设置API KEY?

"驱动程序"构建器没有setJsonHttpRequestInitializer方法.我在哪里可以设置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();
}
Run Code Online (Sandbox Code Playgroud)

获得:

 com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
 {
   "code" : 403,
   "errors" : [ {
     "domain" : "usageLimits",
     "message" : "Access Not Configured",
     "reason" : "accessNotConfigured"
  } ],
  "message" : "Access Not Configured"
 }
Run Code Online (Sandbox Code Playgroud)

pom*_*tee 5

您需要在API控制台中激活两项内容: - Drive API - Drive SDK

很多人只激活一个.

此外,在发布最终应用程序之前,不要忘记添加真实证书SHA1.

有类似的答案: Google Drive SDK Exception