Reputation: 405
I am using Google Sheet API (V4).
I had my google account like "[email protected]". To use google api, I made a project in Google Console and created service account.
I want to create a Google Sheet that can be accessed by only authorized person and myself who create the sheet. The authorized person can read and edit. Either do i.
But whenever I tired my code, it went well and I can get the sheet url. but when I click the url they show I need a permission. It is the url that I created [https://docs.google.com/spreadsheets/d/1RKR-ErUaC_LujUUDf8o_yIprEz223U1EltJ7zYPo7us/edit]
I think the problem is I am using a service account for OAuth. I need to use this.
So.. I want to create google sheet and give a permission to read and edit to person I select.
Please help me out...!
/**
* 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();
}
my Service Account Information is like :
{
"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"
}
Below is my test code to create the sheet :
Spreadsheet requestBody = new Spreadsheet();
Sheets.Spreadsheets.Create request = sheets.spreadsheets().create(requestBody);
Spreadsheet response = request.execute();
System.out.println(response);
Upvotes: 4
Views: 5131
Reputation: 805
You can also create the Google sheet in your personal Google Drive account (not the account of your Google Cloud project/service account) and share access for that file or the folder with your service account, i.e. your Google Cloud project (as well as other accounts if you like).
In the service account file you can download from the Google Console there is a "client_email" property. That's the email address of your service account.
You can use that email address in the Google Drive web interface to share access for a folder or file with your service account and/or another person's Google account.
Upvotes: 2
Reputation: 6791
You are correct, since you are using a service account the file created by it is only accessible to that account. As stated in this tutorial about service account:
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.
To add your self a permission to the file:
Permissions: update
or Permissions: create
I suggest that you create the sheets using Drive API to add permission in the sheet creation. Then use Sheets API to edit the content.
Hope this helps.
Upvotes: 2