1

我有一个 Java 应用程序,它使用 Google Contact API 将联系人与本地 PDA 应用程序同步。它在我的旧机器上运行良好,但是在将源代码和所有内容(包括 client_secrets.json)复制到新笔记本电脑后,我现在调用 com.google.gdata.client.Service.getFeed() 时收到以下错误:

com.google.gdata.util.ServiceForbiddenException:无法请求属于其他用户的联系人

我传递了与以前相同的用户 ID 和密码,所以我不确定为什么它会显示“另一个用户”。

这是我的相关代码:

    // Initialize Contacts service      
    service = new ContactsService(cfg.gAppName);

    // Authorize the service

    Credential credential = authorize(CONTACTS_SCOPE, "contacts");
    if (credential != null && (credential.getExpiresInSeconds() == null || credential.getExpiresInSeconds() < 5)) {
        credential.refreshToken();
    }

    String token = credential.getAccessToken();
    service.setHeader("Authorization", "Bearer " + token);              

    // Request the feed
    contFeedUrl = new URL(BASE_CONT_URL + CONTACTS + cfg.gUser + "/full?max-results=999999");
    if (bFeed) {
        resultFeed = service.getFeed(contFeedUrl, ContactFeed.class);
    }

这是我的 authorize() 方法:

    protected Credential authorize(String scope, String subDir) throws Exception {

    File dataStoreDir = new File(System.getProperty("user.home"), ".store/gsync/" + cfg.dataStore + "/" + subDir);

    // initialize the transport
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    // initialize the data store factory
    dataStoreFactory = new FileDataStoreFactory(dataStoreDir);

    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(SyncMgr.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.out.println(
                "Enter Client ID and Secret from https://code.google.com/apis/console/?api=calendar "
                + "into /client_secrets.json");
        System.exit(1);
    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, JSON_FACTORY, clientSecrets,
            Collections.singleton(scope)).setDataStoreFactory(dataStoreFactory).build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(cfg.gUser);
}

有什么想法可以让它在新机器上工作吗?

4

0 回答 0