Google 人员 API 删除联系人:请求的身份验证范围不足

Google People API Delete Contact: Request had insufficient authentication scopes

我正在尝试更新 Google 人 API 的联系信息,但我收到以下回复:

    Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Request had insufficient authentication scopes.",
    "reason" : "forbidden"
  } ],
  "message" : "Request had insufficient authentication scopes.",
  "status" : "PERMISSION_DENIED"
}

我正在使用以下代码:

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException
 */
protected static Credential authorize() throws IOException {
    // Load client secrets.
    final InputStream in = GooglePeopleAPI.class.getResourceAsStream("/google/client_secret.json");
    final GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();

    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

获取凭据后,我初始化 PeopleService

/**
 * Build and return an authorized People client service.
 * @return an authorized People client service
 */
public static PeopleService getPeopleService() {
    try {
        return new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, authorize())
                .setApplicationName(APPLICATION_NAME)
                .build();
    } catch (IOException ex) {
        throw new GoogleException("", "Não foi possível criar uma instância do PeopleService", ex);
    }
}

最后执行 API 调用

PEOPLE_SERVICE
                    .people()
                    .deleteContact("contact id")
                    .execute();

我可以阅读所有联系人,但我不能delete/update任何一个。我在这里错过了什么?

您的 SCOPES 变量的作用域是什么?您使用的是只读联系人 https://www.googleapis.com/auth/contacts.readonly 范围吗?如果是这样,您需要使用 write contacts 范围进行删除 https://www.googleapis.com/auth/contacts。参见 documentation