要根据this从谷歌驱动器下载文件,我们必须使用
获取https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media 授权:持票人' ACCESS_TOKEN'
我想知道这个访问令牌究竟是什么? 这个Android客户端ID?在Authorization标头中传递客户端ID时,它会给出401未经授权的错误。 如果我使用下面给出的代码,文件下载成功,但当我尝试打开它时,它会显示无效文件
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
答案 0 :(得分:1)
您可以将Drive API Client Library for Java添加到模块build.gradle
中...而不是为该模块创建一个全新的客户端
Google Drive REST API。然后clientId应来自google-services.json
文件(可能与REST凭据不同,请检查控制台)。
dependencies {
compile 'com.google.http-client:google-http-client-android:1.22.0'
compile 'com.google.http-client:google-http-client-jackson2:1.22.0'
compile 'com.google.api-client:google-api-client-android:1.22.0'
compile 'com.google.api-client:google-api-client-jackson2:1.22.0'
compile 'com.google.oauth-client:google-oauth-client:1.22.0'
//
Google Drive API:
compile 'com.google.apis:google-api-services-drive:v3-rev65-1.22.0'
}
然后运行./gradlew clean
下载库。
答案 1 :(得分:0)
阅读this文章后,我开始知道什么是访问令牌 Mobile Api Security
在OAuth2 for Mobile API Security
中用户打开您的移动应用,系统会提示您输入用户名或电子邮件和密码。
您可以使用用户的用户名或电子邮件和密码数据从您的移动应用向您的API服务发送POST请求(超过SSL!)。
您验证用户凭据,并为在一定时间后过期的用户创建访问令牌。
您将此访问令牌存储在移动设备上,将其视为可以访问API服务的API密钥。
一旦访问令牌过期且不再有效,您将重新提示用户输入用户名或电子邮件和密码。