我正在使用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);
謝謝你的回答。有用。我不認爲我需要給我的服務帳戶允許使用我的驅動器。謝謝! –