Google Contacts API failing to retrieve contact information via a service account

404 Views Asked by At

I have been unsuccessful using the Google Contacts API to retrieve contact information from a service account (not using a live user session). No matter what I try, I either get an exception (such as: Message = "Error:\"invalid_request\", Description:\"Invalid impersonation prn email address.\", Uri:\"\""), or no error but 0 contacts returned.

I have double-checked the settings in the Google Apps Admin console and Manage API Client Access pages (including getting verification of those settings from Google App tech support), but regardless, I cannot get contacts to load. I also have tried all of the examples that I could find on "Stack Overflow," but none of those seem to correct the issue.

These attempts have been in C# / ASP.net, but I am conversant enough in other languages that I should be able to adapt any example that somebody might have that works.

I'm hoping somebody has been successful at using Google Contacts API from a service account and would be willing to share the code that they have done to accomplish that.

Thank you!!!

Here is an example of an attempt I made that always results in the exception "invalid_request\": "Invalid impersonation prn email address.\"

    private void TestLoadContacts()
    {
        try
        {
            string strClientID = "STRINGGENERATEDFROMGOOGLEAPPSINTERFACE.apps.googleusercontent.com";
            var credential = GenerateCred(new[] { "https://www.google.com/m8/feeds" }, strClientID);

            // Get the token for this scope and user
            if (credential.RequestAccessTokenAsync(new CancellationToken()).Result)
            {
                // use the token to initalize the request
                var rs = new RequestSettings("Google Sync")
                {
                    OAuth2Parameters = new OAuth2Parameters()
                    {
                        AccessToken = credential.Token.AccessToken
                    }
                };

                var request = new ContactsRequest(rs);
                Feed<Contact> f = request.GetContacts();
                foreach (var c in f.Entries)
                {
                    //process each contact
                }
            }
        }
        catch (Exception ex)
        {
            string strError = ex.Message;
        }
    }

    private static ServiceAccountCredential GenerateCred(IEnumerable<string> scopes, string delegationUser)
    {
        string strServiceAccountEmail = "[email protected]";
        X509Certificate2 certificate = new X509Certificate2(@"C:\MyP12s\MyGoogleAccount-9da1a08f4eef.p12",
            "notasecret", X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(strServiceAccountEmail)
            {
                Scopes = scopes,
                User = delegationUser
            }.FromCertificate(certificate));
        return credential;
    }
1

There are 1 best solutions below

2
adjuremods On

Retrieving contacts should be easily done (as cited at the Google Contacts API documentation)

Is the question more for using Service Accounts itself? If so, you'll need to enable domain-wide delegation to the service account (more information can be found here; which is also linked on the reference above). Basically you'll need to add the specific scopes you'll use to the authorized service account.

Hope this helps!