2017-06-06 237 views
0

我正在使用Google Sheet API(V4)。當服務帳戶創建Google表格時,需要該權限。我如何給予許可?

我有我的谷歌帳戶,如「[email protected]」。要使用google api,我在Google Console中創建了一個項目並創建了服務帳戶。

我想創建一個Google表格,只有授權人員和我自己可以創建該表格。授權人員可以閱讀和編輯。我也不。

但是,每當我厭倦了我的代碼,它進展順利,我可以得到工作表網址。但是當我點擊他們顯示的url時,我需要一個權限。 這是我創建的網址[https://docs.google.com/spreadsheets/d/1RKR-ErUaC_LujUUDf8o_yIprEz223U1EltJ7zYPo7us/edit]

我認爲問題在於我正在使用OAuth的服務帳戶。我需要使用這個。

所以..我想創建谷歌表,並給予我選擇的人閱讀和編輯的權限。

請幫我... ...!

/** 
* Application name. 
*/ 
private static final String APPLICATION_NAME = "Google Sheets API Java"; 

/** 
* Global instance of the JSON factory. 
*/ 
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 
/** 
* Global instance of the HTTP transport. 
*/ 
private static HttpTransport HTTP_TRANSPORT; 

/** 
* Global instance of the scopes required by this quickstart. 
* <p> 
* If modifying these scopes, delete your previously saved credentials 
* at ~/.credentials/sheets.googleapis.com-java-quickstart 
*/ 
private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS); 

static { 
    try { 
     HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
    } catch (Throwable t) { 
     t.printStackTrace(); 
     System.exit(1); 
    } 
} 

/** 
* Creates an authorized Credential object. 
* 
* @return an authorized Credential object. 
* @throws IOException 
*/ 
public Credential authorize() throws IOException { 
    // Load client secrets. 
    InputStream in = GoogleSheetConfig.class.getResourceAsStream("/My Service Account Information.json"); 
    GoogleCredential credential = GoogleCredential 
      .fromStream(in) 
      .createScoped(SCOPES); 
    return credential; 
} 

/** 
* Build and return an authorized Sheets API client service. 
* 
* @return an authorized Sheets API client service 
* @throws IOException 
*/ 
@Bean 
public Sheets getSheetsService() throws Exception { 
    Credential credential = authorize(); 
    return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) 
      .setApplicationName(APPLICATION_NAME) 
      .build(); 
} 

我的服務帳戶信息是這樣的:

{ 
"type": "service_account", 
"project_id": "my project id ", 
"private_key_id": "fb925c0954********", 
"private_key": 
"client_email": "[email protected] 
test.iam.gserviceaccount.com", 
"client_id": "my client id", 
"auth_uri": "https://accounts.google.com/o/oauth2/auth", 
"token_uri": "https://accounts.google.com/o/oauth2/token", 
"auth_provider_x509_cert_url": 
"https://www.googleapis.com/oauth2/v1/certs", 
"client_x509_cert_url": 
"https://www.googleapis.com/robot/v1/metadata/x509/admin-auth-test- 
service-accoun%40admin-auth-test.iam.gserviceaccount.com" 
} 

下面是我的測試代碼來創建表:

Spreadsheet requestBody = new Spreadsheet(); 

Sheets.Spreadsheets.Create request = sheets.spreadsheets().create(requestBody); 

Spreadsheet response = request.execute(); 
System.out.println(response); 

回答

0

你是正確的,因爲你使用的服務帳戶由其創建的文件只能通過該帳戶訪問。在本tutorial有關服務帳戶說:

使用service account訪問谷歌雲端硬盤API可以是非常有用的,但需要記住的是一個服務帳戶是不是你很重要。如果您希望能夠訪問上傳的文件,則必須通過服務帳戶授予您訪問權限的權限。

要增加你的自我許可文件:

  1. 使用驅動器API
  2. 使用Permissions: updatePermissions: create

我建議你使用驅動API添加創建表工作表創建權限。然後使用表格API編輯內容。

希望這會有所幫助。

+1

謝謝你的回答。有用。我不認爲我需要給我的服務帳戶允許使用我的驅動器。謝謝! –

相關問題