androidtester
androidtester

Reputation: 973

Google Contacts API (contacts.readonly)

I'm developing a C Library to access my google contacts information to be used in a C command line app (for personal use only). I'm trying to authenticate with the scope: https://www.googleapis.com/auth/contacts.readonly but the answer is always "invalid_scope". Any suggestion?

EDIT: Some more information about my problem. I created a Google Project and enabled some APIs (Contacts API and People API). I'm using curl and a JSON library to communicate with Google APIs. The code that I'm using is:

#define GOOGLE_AUTH_URL "https://accounts.google.com/o/oauth2/device/code"
#define GOOGLE_AUTH_POST "client_id="GOOGLE_AUTH_CLIENT_ID"&scope=email profile https://www.googleapis.com/auth/contacts.readonly"

int main(void) {
    char * res = handle_url(GOOGLE_AUTH_URL,GOOGLE_AUTH_POST); // use curl to make a POST

    if (res==NULL) {
        Report("Error");
        return -1;
    }
    cJSON *obj = cJSON_Parse(res);
    printf("Result=%s\n",cJSON_Print(obj));
    return 0;
}

... and the result is:

Result={
        "error":        "invalid_scope"
}

If I change the define to:

#define GOOGLE_AUTH_POST "client_id="GOOGLE_AUTH_CLIENT_ID"&scope=email profile https://www.googleapis.com/auth/contacts"

the result is:

Result={
        "verification_url":     "https://www.google.com/device",
        "expires_in":   1800,
        "interval":     5,
        "device_code":  "AH-1Ng2lAE01qw5HFlGQqT02q7dtmAE6WmKJ_FkH0mO4enJMybvCvRzXnazvyUm22-sJR51ZtKkIJjOT-QhO0PJBUZpCdLrOEw",
        "user_code":    "JNXW-WQPJ"
}

Upvotes: 1

Views: 4086

Answers (2)

Kant Chen
Kant Chen

Reputation: 403

As Graeme said, first you need to enable the API. Second, in your OAuth client setting page (Credential -> OAuth 2.0 Client IDs section), you also need to explicitly add this scope https://www.googleapis.com/auth/contacts.readonly.

I guess the reason why you can use this scope (https://www.googleapis.com/auth/contacts) is because you already added this scope on the OAuth client setting page.

Upvotes: 0

Graeme
Graeme

Reputation: 990

Google wants developers to explicitly enable the APIs that they intend to use. This limits the potential damage if your OAuth credentials are compromised. So the Contacts API may need to be enabled for your project.

Visit this link and select the project from the page header to confirm that the API is enabled for your project: https://console.developers.google.com/apis/api/contacts.googleapis.com/overview

Upvotes: 0

Related Questions