簡體   English   中英

我的應用程序無法在谷歌驅動器的“appDataFolder”中創建文件

[英]My app can not create file in “appDataFolder” of google drive

如何在谷歌驅動器“appDataFolder”中創建文件?

我創建了創建文件和訪問“appDataFolder”所必需的谷歌開發者控制台設置。

谷歌開發者控制台

Android Studio output 錯誤信息如下。

W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
    {
      "code": 403,
      "errors": [
        {
          "domain": "global",
          "message": "The granted scopes do not allow use of the Application Data folder.",
          "reason": "insufficientScopes"
        }
      ],
      "message": "The granted scopes do not allow use of the Application Data folder."
    }
W/System.err:     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:432)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at com.example.test1.MainActivity$3.run(MainActivity.java:131)
        at java.lang.Thread.run(Thread.java:923)

源代碼是

                        File body = new File();
                        body.setName(FILE_TITLE);//fileContent.getName());
                        body.setMimeType("text/plain");
                        body.setParents(Collections.singletonList("appDataFolder"));

                        File file = service.files().create(body, content)
                                .setFields("id")
                                .execute();

如果以下行在源代碼中被注釋掉,我的應用程序可以在谷歌驅動器頂部文件夾中創建一個文件,而不是“appDataFolder”。

body.setParents(Collections.singletonList("appDataFolder"));

Android Studio 4.1.2在此處輸入圖像描述

此致。 提前致謝。

從您的開發人員控制台圖像的外觀來看,您似乎包含大量范圍。 這里的問題不在於您在谷歌開發者控制台中的設置,而在於您已授權用戶使用的范圍。

當您運行您的應用程序時,用戶被要求同意您訪問他們的一些數據,這是您的應用程序的授權 scope。 它定義了用戶授予您的應用程序訪問權限的內容。

如果您的代碼只請求只讀訪問,然后您嘗試寫入它,那么您將看到一個不足的Scopes 錯誤消息。

您需要檢查您使用的方法需要什么 scope,然后確保在您授權用戶時請求 scope。

我的猜測是你有這樣的東西

private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);

您需要包含此 scope https://www.googleapis.com/auth/drive.appdata這可能類似於DriveScopes.DRIVE_APPDATA

請記住,一旦在您的應用程序中更改了范圍,您將需要重新授權用戶。 如果您存儲同意,則需要將其刪除才能再次請求授權。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM