2017-10-16 121 views
0

我正在嘗試使用服務帳戶獲取我的Google雲項目中的存儲桶列表。Google雲端存儲 - 存儲列表錯誤:403禁止訪問

下面是程序:

package com.mypackage.api; 

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.json.jackson2.JacksonFactory; 

import java.io.IOException; 
import java.security.GeneralSecurityException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

import com.google.api.services.storage.Storage; 
import com.google.api.services.storage.StorageScopes; 
import com.google.api.services.storage.model.Bucket; 

public class TestClass { 
    public static void main(String args[]) throws IOException, GeneralSecurityException { 
     String project = "my-project"; 

     Storage storage = createStorageService(); 
     List<String> list = new ArrayList<String>(); 
     List<Bucket> buckets = storage.buckets().list(project).execute().getItems(); 
     if(buckets != null) { 
      for(Bucket b : buckets) { 
       list.add(b.getName()); 
      } 
     } 


    } 

    public static Storage createStorageService() throws IOException, GeneralSecurityException { 
     HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
     JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 


     GoogleCredential credential = GoogleCredential.getApplicationDefault(); 
     if (credential.createScopedRequired()) { 
      credential = credential.createScoped(Arrays.asList(StorageScopes.DEVSTORAGE_FULL_CONTROL, "https://www.googleapis.com/auth/cloud-platform")); 
     } 

     return new Storage.Builder(httpTransport, jsonFactory, 
       credential).setApplicationName("My Application") 
       .build(); 

    } 
} 

而且我得到了下面的錯誤:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden 
{ 
    "code" : 403, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "[email protected] does not have storage.buckets.list access to project 333XXXXX67.", 
    "reason" : "forbidden" 
    } ], 
    "message" : "[email protected] does not have storage.buckets.list access to project 333XXXXX67." 
} 

我已經加入 「所有者」 的範圍,以我的服務帳戶。但是我仍然得到了許可被拒絕的錯誤。

任何幫助表示讚賞。

回答

0

我通過更改憑據類型文件糾正了此錯誤。最初,我把Web應用程序文件,所以這就是爲什麼我得到這個錯誤。感謝您回覆我的問題。

相關問題