gpt4 book ai didi

java - 未能创建 google+ 客户端对象以检索 Java 中的 Activity

转载 作者:行者123 更新时间:2023-12-04 05:04:02 26 4
gpt4 key购买 nike

我是 Google+ API 和 Java-Client API 的新手。所以请耐心等待我。

我试图使用 Java-Client 和 G+ APIs 1.14 检索我的 google+ Activity ,但我未能创建客户端对象,我已经尝试了一天,但我没有运气解决它我自己所以我来这里寻求帮助。

我的代码是:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.services.CommonGoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.json.CommonGoogleJsonClientRequestInitializer;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.model.Activity;
import com.google.api.services.plus.model.ActivityFeed;
import com.google.api.services.plus.Plus.Builder;

public class GooglePlusService {

/**
* google plus service acess stub
*/
private Plus plusSvc;

/**
* google plus api key
*/
private String GooglePlusAPIKey = "MY key"; //<-- YOUR KEY GOES HERE

/**
* connect to the google plus service
*/
public GooglePlusService() {

setupTransport();

}

public List<Activity> getActivityList(String userid) throws IOException {

List<Activity> retval = new ArrayList<Activity>();

Plus.Activities.List listActivities =
plusSvc.activities().list(userid, "public");

listActivities.setMaxResults(100L);

// get the 1st page of activity objects
ActivityFeed activityFeed = listActivities.execute();

// unwrap the request and extract the pieces we want
List<Activity> pageOfActivities = activityFeed.getItems();

// loop through until we arrive at an empty page
while (pageOfActivities != null) {
for (Activity activity : pageOfActivities) {
retval.add(activity);
System.out.println("ID " + activity.getId() + " Content: " +
activity.getObject().getContent());
}

// we will know we are on the last page when the next page token
// is null (in which case, break).
if (activityFeed.getNextPageToken() == null) {
break;
}

// prepare to request the next page of activities
listActivities.setPageToken(activityFeed.getNextPageToken());

// execute and process the next page request
activityFeed = listActivities.execute();
pageOfActivities = activityFeed.getItems();
}

return retval;

}

/**
* google plus service stub object
*/
public Plus getPlusSvc() {
return plusSvc;
}

private void setupTransport() {

/*
plusSvc = new Plus(new NetHttpTransport(),
new GsonFactory());
plusSvc.setKey(GooglePlusAPIKey);

*/


HttpRequestInitializer initializer = (HttpRequestInitializer) new CommonGoogleClientRequestInitializer("GooglePlusAPIKey");

plusSvc = new Plus(new NetHttpTransport(), new GsonFactory(), initializer);

GoogleClientRequestInitializer KEY_INITIALIZER = new CommonGoogleJsonClientRequestInitializer(GooglePlusAPIKey);
}


}

我的问题是在 setupTransport() 部分,我未能创建客户端对象,因为我不知道在以下位置将什么分配给 httpRequestInitializer:

加号(HttpTransport 传输,JsonFactory jsonFactory,HttpRequestInitializer httpRequestInitializer)

我试过:
HttpRequestInitializer initializer = (HttpRequestInitializer)new CommonGoogleClientRequestInitializer("GooglePlusAPIKey");
但它给出了无法转换 CommonGoogleClientRequestInitializer 的错误

我试过:
GoogleClientRequestInitializer KEY_INITIALIZER = new CommonGoogleJsonClientRequestInitializer(GooglePlusAPIKey);
但是后来不知道怎么办了。

我需要了解 http 请求初始化程序如何使用 API

我真的需要帮助,我很感激。

等待回应。

最佳答案

HttpRequestInitializer 实例是一个 GoogleCredential 对象,如 PhotoHunt sample 中所示。 .在发送请求之前,GoogleCredential 对象会在 Plus 服务的每个请求上设置相关的 Authorization header 。

创建 GoogleCredential 对象并在 Plus.Builder 中使用它的相关行是:

GoogleCredential credential = new GoogleCredential.Builder()
.setJsonFactory(JSON_FACTORY).setTransport(TRANSPORT)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET).build();
GoogleTokenResponse tokenFromExchange = exchangeCode(accessToken);
credential.setFromTokenResponse(tokenFromExchange);
Plus plus = new Plus.Builder(TRANSPORT, JSON_FACTORY, credential).build();

关于java - 未能创建 google+ 客户端对象以检索 Java 中的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15749926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com