账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    开发 Android 应用,如何使用 Google Drive API 上传文件?
    18
    0

    已经开启了 Google Drive API,按照
    以下页面的示例,仍然无法上传。
    app 的代码应该没有问题,是 api 方面还有什么没有开启或没有设置好么?
    查了下,有人说要同时开启 drive api
    和 drive sdk,但貌似 drive sdk 现在已经没有了。
    https://developers.google.com...

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 放ジ荡〆 普通会员 1楼

      要使用 Google Drive API 上传文件,您需要使用 OAuth 2.0 身份验证。以下是在 Android 应用中使用 Google Drive API 上传文件的步骤:

      1. 创建 OAuth 2.0 服务账户并为其分配密钥:

      请访问 https://console.cloud.google.com/appengine/admin/ 获得您的服务账户。

      在 Google Cloud Console 中,创建一个新的 OAuth 2.0 服务账户。为服务账户授予所需权限,并为每个需要访问 Google Drive API 的应用程序分配密钥。

      1. 添加依赖:

      在您的 Android 应用的 build.gradle 文件中添加以下依赖:

      implementation 'com.google.api.core:gson:2.8.0' implementation 'com.google.dagger:dagger:2.11.2' implementation 'com.google.dagger:dagger-compiler:2.11.2'

      1. 创建 Google Drive API 服务端:

      使用以下代码创建 Google Drive API 服务端:

      ```java @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

         // 获取 OAuth 2.0 服务账户密钥
         String clientKey = getGoogleDriveClientKey();
         String clientSecret = getGoogleDriveClientSecret();
      
         // 生成 OAuth 2.0 令牌
         String accessToken = generateAccessToken();
      
         // 从服务器获取文件
         String fileUrl = "https://drive.google.com/file/d/your_file_name/view?usp=sharing";
         String fileContent = uploadFile(fileUrl, accessToken);
      

      }

      private String getGoogleDriveClientKey() { // 获取 OAuth 2.0 服务账户密钥 return "your_client_key"; }

      private String getGoogleDriveClientSecret() { // 获取 OAuth 2.0 服务账户密钥 return "your_client_secret"; }

      private String generateAccessToken() { // 生成 OAuth 2.0 令牌 return "your_access_token"; }

      private void uploadFile(String fileUrl, String accessToken) { // 上传文件 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(fileUrl)); intent.putExtra("Authorization", "Bearer " + accessToken); startActivity(intent); } ```

      1. 更新 Android 应用的代码以获取文件 URL:

      在您的 Android 应用的 onCreate 方法中,从服务器获取文件 URL 并将其传递给 uploadFile 方法。

      1. 实现上传文件逻辑:

      在 uploadFile 方法中,从文件 URL 中获取文件内容,并将其存储到本地存储中。最后,将文件上传到 Google Drive。

      ```java private void uploadFile(String fileUrl, String accessToken) { // 从文件 URL 中获取文件内容 String fileContent = fileUrl.substring(fileUrl.lastIndexOf("/") + 1); String fileResource = "file资源名"; String fileContentResource = "file内容资源名"; byte[] fileContentBytes = fileContent.getBytes("UTF-8");

         // 将文件内容存储到本地存储中
         try (FileOutputStream fos = new FileOutputStream(fileResource)) {
             fos.write(fileContentBytes);
         } catch (IOException e) {
             e.printStackTrace();
         }
      
         // 将文件内容上传到 Google Drive
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(fileUrl));
         intent.putExtra("Authorization", "Bearer " + accessToken);
         startActivity(intent);
      

      } ```

      1. 测试上传文件功能:

      在 Android 环境中测试上传文件功能。使用上传文件 API 的 ACTION_VIEW 并指定文件 URL。确保上传成功。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部