当服务帐户创建 Google 表时,需要权限。我怎样才能给予许可?
When Service Account creates Google Sheets,The permission is needed. How can I give a permission?
我正在使用 Google Sheet API (V4)。
我的 google 帐户类似于 "test@google.com"。为了使用 google api,我在 Google 控制台中创建了一个项目并创建了服务帐户。
我想创建一个 Google Sheet,只能由创建 sheet 的授权人和我自己访问。授权人可以阅读和编辑。我也不。
但是每当我累了我的代码时,它都很顺利,我可以得到 sheet url。但是当我点击 url 时,他们显示我需要许可。
就是我创造的url [https://docs.google.com/spreadsheets/d/1RKR-ErUaC_LujUUDf8o_yIprEz223U1EltJ7zYPo7us/edit]
我认为问题在于我正在使用 OAuth 服务帐户。我需要用这个。
所以..我想创建 google sheet 并授予我 select 的人阅读和编辑的权限。
请帮帮我...!
/**
* 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": "test-service-accoun@admin-auth-
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"
}
下面是我创建 sheet 的测试代码:
Spreadsheet requestBody = new Spreadsheet();
Sheets.Spreadsheets.Create request = sheets.spreadsheets().create(requestBody);
Spreadsheet response = request.execute();
System.out.println(response);
你是对的,因为你使用的是服务帐户,它创建的文件只能由该帐户访问。如本 tutorial 关于服务帐户所述:
Using a service account to access Google Drive API can be very useful but it is important to remember that a service account is not you. If you want to be able to access the files it uploads you must grant yourself access to them though the service account.
要为自己添加对文件的权限:
- 使用驱动器API
- 使用
Permissions: update
or Permissions: create
我建议您使用驱动器 API 创建 sheet 以在 sheet 创建中添加权限。然后使用表格API编辑内容。
希望这对您有所帮助。
您还可以在您的个人 Google Drive 帐户(不是您的 Google Cloud project/service 帐户)中创建 Google sheet 并且与您的服务帐户共享对该文件或文件夹的访问权限,即您的 Google 云项目(以及其他帐户,如果您愿意)。
在您可以从 Google 控制台下载的服务帐户文件中,有一个 "client_email" 属性。这是您的服务帐户的电子邮件地址。
您可以在 Google 云端硬盘网络界面中使用该电子邮件地址与您的服务帐户 and/or 另一个人的 Google 帐户共享文件夹或文件的访问权限。
我正在使用 Google Sheet API (V4)。
我的 google 帐户类似于 "test@google.com"。为了使用 google api,我在 Google 控制台中创建了一个项目并创建了服务帐户。
我想创建一个 Google Sheet,只能由创建 sheet 的授权人和我自己访问。授权人可以阅读和编辑。我也不。
但是每当我累了我的代码时,它都很顺利,我可以得到 sheet url。但是当我点击 url 时,他们显示我需要许可。 就是我创造的url [https://docs.google.com/spreadsheets/d/1RKR-ErUaC_LujUUDf8o_yIprEz223U1EltJ7zYPo7us/edit]
我认为问题在于我正在使用 OAuth 服务帐户。我需要用这个。
所以..我想创建 google sheet 并授予我 select 的人阅读和编辑的权限。
请帮帮我...!
/**
* 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": "test-service-accoun@admin-auth-
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"
}
下面是我创建 sheet 的测试代码:
Spreadsheet requestBody = new Spreadsheet();
Sheets.Spreadsheets.Create request = sheets.spreadsheets().create(requestBody);
Spreadsheet response = request.execute();
System.out.println(response);
你是对的,因为你使用的是服务帐户,它创建的文件只能由该帐户访问。如本 tutorial 关于服务帐户所述:
Using a service account to access Google Drive API can be very useful but it is important to remember that a service account is not you. If you want to be able to access the files it uploads you must grant yourself access to them though the service account.
要为自己添加对文件的权限:
- 使用驱动器API
- 使用
Permissions: update
orPermissions: create
我建议您使用驱动器 API 创建 sheet 以在 sheet 创建中添加权限。然后使用表格API编辑内容。
希望这对您有所帮助。
您还可以在您的个人 Google Drive 帐户(不是您的 Google Cloud project/service 帐户)中创建 Google sheet 并且与您的服务帐户共享对该文件或文件夹的访问权限,即您的 Google 云项目(以及其他帐户,如果您愿意)。
在您可以从 Google 控制台下载的服务帐户文件中,有一个 "client_email" 属性。这是您的服务帐户的电子邮件地址。
您可以在 Google 云端硬盘网络界面中使用该电子邮件地址与您的服务帐户 and/or 另一个人的 Google 帐户共享文件夹或文件的访问权限。