我正在开发一个C库来访问我的谷歌联系人信息,以便在C命令行应用程序中使用(仅供个人使用(。我正在尝试使用范围进行身份验证:https://www.googleapis.com/auth/contacts.readonly 但答案始终是"invalid_scope"。有什么建议吗?
编辑:有关我的问题的更多信息。我创建了一个 Google 项目并启用了一些 API(联系人 API 和人员 API(。我正在使用curl和JSON库与Google API进行通信。我使用的代码是:
#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=%sn",cJSON_Print(obj));
return 0;
}
。结果是:
Result={
"error": "invalid_scope"
}
如果我将定义更改为:
#define GOOGLE_AUTH_POST "client_id="GOOGLE_AUTH_CLIENT_ID"&scope=email profile https://www.googleapis.com/auth/contacts"
结果是:
Result={
"verification_url": "https://www.google.com/device",
"expires_in": 1800,
"interval": 5,
"device_code": "AH-1Ng2lAE01qw5HFlGQqT02q7dtmAE6WmKJ_FkH0mO4enJMybvCvRzXnazvyUm22-sJR51ZtKkIJjOT-QhO0PJBUZpCdLrOEw",
"user_code": "JNXW-WQPJ"
}
Google 希望开发者明确启用他们打算使用的 API。这限制了 OAuth 凭据泄露时的潜在损害。因此,可能需要为项目启用联系人 API。
访问此链接并从页眉中选择项目,以确认已为您的项目启用 API:https://console.developers.google.com/apis/api/contacts.googleapis.com/overview
正如 Graeme 所说,首先你需要启用 API。其次,在 OAuth 客户端设置页面( Credential
-> OAuth 2.0 Client IDs
部分(中,还需要显式添加此范围https://www.googleapis.com/auth/contacts.readonly
。
我想您可以使用此范围(https://www.googleapis.com/auth/contacts
(的原因是您已经在OAuth客户端设置页面上添加了此范围。