Search code examples
pythonrestgoogle-people-apigoogle-shared-contacts

Domain Shared Contacts API to create external contacts using python + service account (client library)


I want to use Domain Shared Contacts API as part of python client library.

The flow that I use in other cases: Create credentials:

credentials = service_account.Credentials.from_service_account_info(....)

'Create' a service object which I execute later

service = googleapiclient.discovery.build(service_name, api_version,credentials=credentials)

In the case of Domain Shared Contacts API I dont know what service_name or api_version to use Google API Discovery Service if any. Is it possible to create/update/remove external contacts for a domain using the contact OR people APIs?

If not, the process of utilising this API is to create requests in your codebase Using OAuth 2.0 for Web Server Applications to REST endpoints like: https://www.google.com/m8/feeds/contacts/example.com/full


Solution

  • I managed to solve my problem only by using the Domain Shared Contacts API

    1. I created a service account.

    2. Prepared an authorized (HTTP/REST) API call using the example here. Special attention to Additional claims sub.

    3. Since I am using Python, I installed pyjwt and used it to create and sign my JWT. As a secret, I used the service_account.private_key:

      jwt.encode(jwt_claim_set, secret, algorithm="RS256")
      

    Then depending on the use case (get contacts, create one..). I assigned the Google token (signed JWT) to my request.

    Example:

    endpoint = 'https://www.google.com/m8/feeds/contacts/{}/{}'.format(your_own_domain, projection_value)
    headers = {"Authorization": "Bearer " + token}
    gsuite_get_response = requests.get(endpoint, headers=headers)