Java 如何在google plus中从圈中检索帖子
我正试图从Google Plus圈子中检索帖子 我已经成功地检索了用户配置文件中的帖子和用户配置文件详细信息 你能帮帮我吗 提前谢谢 我的代码是Java 如何在google plus中从圈中检索帖子,java,google-app-engine,oauth-2.0,Java,Google App Engine,Oauth 2.0,我正试图从Google Plus圈子中检索帖子 我已经成功地检索了用户配置文件中的帖子和用户配置文件详细信息 你能帮帮我吗 提前谢谢 我的代码是 package main.java; import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource; import com.google.api.client.http.HttpResponseException;
package main.java;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.json.JsonHttpRequest;
import com.google.api.client.http.json.JsonHttpRequestInitializer;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.PlusRequest;
import com.google.api.services.plus.model.*;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
public class Sample {
private static final Logger log = Logger.getLogger(Sample.class.getName());
private static Plus plus;
private static Plus unauthenticatedPlus;
public static void main(String[] args) throws IOException {
try {
setupTransport();
getProfile();
listActivities();
getActivity();
} catch (HttpResponseException e) {
log.severe(e.getResponse().parseAsString());
throw e;
}
}
/**
* Setup the transport for our API calls.
* @throws java.io.IOException when the transport cannot be created
*/
private static void setupTransport() throws IOException {
// Here's an example of an unauthenticated Plus object. In cases where you
// do not need to use the /me/ path segment to discover the current user's
// ID, you can skip the OAuth flow with this code.
unauthenticatedPlus = Plus.builder(Util.TRANSPORT, Util.JSON_FACTORY)
// When we do not specify access tokens, we must specify our API key instead
// We do this using a JsonHttpRequestInitializer
.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest jsonHttpRequest) throws IOException {
PlusRequest plusRequest = (PlusRequest) jsonHttpRequest;
plusRequest.setKey(Auth.GOOGLE_API_KEY);
}
}).build();
// If, however, you need to use OAuth to identify the current user you must
// create the Plus object differently. Most programs will need only one
// of these since you can use an authenticated Plus object for any call.
Auth.authorize();
GoogleAccessProtectedResource requestInitializer =
new GoogleAccessProtectedResource(
Auth.getAccessToken(),
Util.TRANSPORT,
Util.JSON_FACTORY,
Auth.CLIENT_ID,
Auth.CLIENT_SECRET,
Auth.getRefreshToken());
plus = Plus.builder(Util.TRANSPORT, Util.JSON_FACTORY)
.setHttpRequestInitializer(requestInitializer).build();
}
/**
* List the public activities for the authenticated user
*
* @throws IOException if unable to call API
*/
private static void listActivities() throws IOException {
header("Search Activities for teja mariduvb");
// Fetch the first page of activities
Plus.Activities.Search listActivities = plus.activities().search();
listActivities.setQuery("teja mariduvb");
listActivities.setMaxResults(20L);
ActivityFeed feed;
try {
feed = listActivities.execute();
} catch (HttpResponseException e) {
log.severe(Util.extractError(e));
throw e;
}
// Keep track of the page number in case we're listing activities
// for a user with thousands of activities. We'll limit ourselves
// to 5 pages
int currentPageNumber = 0;
while (feed != null && feed.getItems() != null && currentPageNumber < 5) {
currentPageNumber++;
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~~~ page "+currentPageNumber+" of activities ~~~~~~~~~~~~~~~~~~");
System.out.println();
for (Activity activity : feed.getItems()) {
show(activity);
System.out.println();
System.out.println("------------------------------------------------------");
System.out.println();
}
// Fetch the next page
// System.out.println("next token: " + feed.getNextPageToken());
// listActivities.setPageToken(feed.getNextPageToken());
// feed = listActivities.execute();
}
}
/**
* Get the most recent activity for the authenticated user.
*
* @throws IOException if unable to call API
*/
private static void getActivity() throws IOException {
// A known public activity ID
String activityId = "z12gtjhq3qn2xxl2o224exwiqruvtda0i";
// We do not need to be authenticated to fetch this activity
header("Get an explicit public activity by ID");
try {
Activity activity = unauthenticatedPlus.activities().get(activityId).execute();
show(activity);
} catch (HttpResponseException e) {
log.severe(Util.extractError(e));
throw e;
}
}
/**
* Get the profile for the authenticated user.
*
* @throws IOException if unable to call API
*/
private static void getProfile() throws IOException {
header("Geting your Google+ profile information here");
try {
Person profile = plus.people().get("me").execute();
show(profile);
} catch (HttpResponseException e) {
log.severe(Util.extractError(e));
throw e;
}
}
/**
* Print the specified person on the command line.
*
* @param person the person to show
*/
private static void show(Person person) {
System.out.println("id: " + person.getId());
System.out.println("name: " + person.getDisplayName());
System.out.println("image url: " + person.getImage().getUrl());
System.out.println("profile url: " + person.getUrl());
System.out.println("AboutMe: " + person.getAboutMe());
System.out.println("RelationshipStatus: " + person.getRelationshipStatus());
System.out.println("Tagline: " + person.getTagline());
System.out.println("PlacesLived: " + person.getPlacesLived());
System.out.println("Birthday: " + person.getBirthday());
}
/**
* Print the specified activity on the command line.
*
* @param activity the activity to show
* @throws IOException
*/
private static void show(Activity activity) throws IOException {
System.out.println("Id of post: " + activity.getId());
System.out.println("Url of post: " + activity.getUrl());
System.out.println("Content of post is: " + activity.getPlusObject().getContent());
System.out.println("Attachments: " + activity.getPlusObject().getAttachments());
System.out.println("No of replies: " + activity.getPlusObject().getReplies().getTotalItems());
System.out.println("Title of post: " + activity.getTitle());
System.out.println("Kind of post: " + activity.getKind());
System.out.println("Actor of post: " + activity.getActor());
System.out.println("Published time of post: " + activity.getPublished());
System.out.println("Updated time: " + activity.getUpdated());
Plus.Comments.List listComments = plus.comments().list(activity.getId());
CommentFeed commentFeed = listComments.execute();
List<Comment> comments = commentFeed.getItems();
System.out.println("\n");
for (Comment comment : comments)
{
System.out.println("Comment Id is: " +comment.getId());
System.out.println("Content of Comment is: " +comment.getPlusObject().getContent());
System.out.println("Comment written by: " +comment.getActor());
System.out.println("Published time of Comment is: " +comment.getPublished());
System.out.println("\n");
}
}
private static void header(String name) {
System.out.println();
System.out.println("============== " + name + " ==============");
System.out.println();
}
}
package main.java;
导入com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
导入com.google.api.client.http.HttpResponseException;
导入com.google.api.client.http.json.JsonHttpRequest;
导入com.google.api.client.http.json.JsonHttpRequestInitializer;
导入com.google.api.services.plus.plus;
导入com.google.api.services.plus.PlusRequest;
导入com.google.api.services.plus.model.*;
导入java.io.IOException;
导入java.util.List;
导入java.util.logging.Logger;
公共类样本{
私有静态最终记录器log=Logger.getLogger(Sample.class.getName());
私人静态加号;
私有静态Plus未经验证Plus;
公共静态void main(字符串[]args)引发IOException{
试一试{
setupTransport();
getProfile();
listActivities();
getActivity();
}捕获(HttpResponseException e){
严重(如getResponse().parseAsString());
投掷e;
}
}
/**
*为我们的API调用设置传输。
*@在无法创建传输时引发java.io.IOException
*/
私有静态void setupTransport()引发IOException{
//下面是一个未经验证的Plus对象的示例
//不需要使用/me/path段来发现当前用户的
//ID,您可以使用此代码跳过OAuth流。
unauthenticatedPlus=Plus.builder(Util.TRANSPORT,Util.JSON_工厂)
//当我们不指定访问令牌时,我们必须指定API密钥
//我们使用JsonHttpRequestInitializer来实现这一点
.setJsonHttpRequestInitializer(新的JsonHttpRequestInitializer(){
@凌驾
public void initialize(JsonHttpRequest JsonHttpRequest)引发IOException{
PlusRequest PlusRequest=(PlusRequest)jsonHttpRequest;
plusRequest.setKey(Auth.GOOGLE\u API\u KEY);
}
}).build();
//但是,如果需要使用OAuth来标识当前用户,则必须
//以不同的方式创建加号对象。大多数程序只需要一个加号对象
//因为您可以对任何调用使用经过身份验证的Plus对象。
Auth.authorize();
GoogleAccessProtectedResource请求初始值设定项=
新GoogleAccessProtectedResource(
Auth.getAccessToken(),
交通工具,
Util.JSON_工厂,
Auth.CLIENT\u ID,
Auth.CLIENT_SECRET,
Auth.getRefreshToken());
plus=plus.builder(Util.TRANSPORT,Util.JSON_工厂)
.setHttpRequestInitializer(请求初始值设定项).build();
}
/**
*列出已验证用户的公共活动
*
*@如果无法调用API,则引发IOException
*/
私有静态void listActivities()引发IOException{
标题(“teja mariduvb搜索活动”);
//获取活动的第一页
Plus.Activities.Search listActivities=Plus.Activities().Search();
setQuery(“teja mariduvb”);
listActivities.setMaxResults(20L);
活动饲料;
试一试{
feed=listActivities.execute();
}捕获(HttpResponseException e){
严重日志(实用提取错误(e));
投掷e;
}
//记录页码,以防我们列出活动
//对于拥有数千项活动的用户,我们将限制自己
//至5页
int currentPageNumber=0;
while(feed!=null&&feed.getItems()!=null&¤tPageNumber<5){
currentPageNumber++;
System.out.println();
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”活动的当前页码+”;
System.out.println();
对于(活动:feed.getItems()){
表演(活动);
System.out.println();
System.out.println(“--------------------------------------------------------------”);
System.out.println();
}
//取下一页
//System.out.println(“下一个标记:+feed.getNextPageToken());
//setPageToken(feed.getNextPageToken());
//feed=listActivities.execute();
}
}
/**
*获取经过身份验证的用户的最新活动。
*
*@如果无法调用API,则引发IOException
*/
私有静态void getActivity()引发IOException{
//已知的公共活动ID
字符串活动ID=“z12gtjhq3qn2xxl2o224exwiqruvtda0i”;
//获取此活动不需要对我们进行身份验证
标题(“通过ID获取明确的公共活动”);
试一试{
活动活动=未经身份验证的plus.activities().get(activityId.execute();
表演(活动);
}捕获(HttpResponseException e){
严重日志(实用提取错误(e));
投掷e;
}
}
/**
*获取经过身份验证的用户的配置文件。
*
*@如果无法调用API,则引发IOException
*/
私有静态void getProfile()引发IOException{
标题(“在此处获取您的Google+个人资料信息”);
试一试{
Person profile=plus.people().get(“me”).execute();
显示(个人资料);
}捕获(HttpResponseException e){
严重日志(实用提取错误(e));
投掷e;
}
}
/**
*在命令l上打印指定的人员