Sampath Kumar
Sampath Kumar

Reputation: 41

Need to update Google contacts of another user using Service Account

I have created a service account with which I am able to update Google Sheets. For this, I had to share the google sheet with the service account email id. Now, I want to be able to update the contacts for the user. When I use the credentials, it is updating for the service account user and not the original user. Is there any way to update this using the service account?

Upvotes: 0

Views: 226

Answers (2)

Teyam
Teyam

Reputation: 8082

You may want to try using Google Contacts API version 3.0. This API allows client applications to view and update a user's contacts.

However, please do note that, to be able to use Google Contacts API, you need to use OAuth 2.0 to authorize requests. And, for server-to-server interactions, you may use OAuth 2.0 for Server to Server Applications wherein it was mentioned that,

If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account.

In delegating domain-wide authority:

If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account with the setServiceAccountUser method of the GoogleCredential factory.

Please note that you need to use the GoogleCredential object to call Google APIs in your application.

Here's an example on how to delegate domain-wide authority:

GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId(emailAddress)
    .setServiceAccountPrivateKeyFromP12File(new File("MyProject.p12"))
    .setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
    .setServiceAccountUser("[email protected]")
    .build();

After obtaining the proper authorization, you may continue with updating contacts.

Hope that helps!

Upvotes: 1

Blake O'Hare
Blake O'Hare

Reputation: 1880

No. Contacts can only be edited with a valid authentication session from the account you want to update.

Upvotes: 0

Related Questions