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

标签 java google-api google-plus google-api-java-client google-api-client

我不熟悉 Google+ API 和 Java-Client API。所以请耐心等待我。

我正在尝试使用 Java-Client 和 G+ API 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 的内容:

Plus(HttpTransport 传输,JsonFactory jsonFactory,HttpRequestInitializer httpRequestInitializer)

我尝试过: HttpRequestInitializer 初始值设定项 = (HttpRequestInitializer)new CommonGoogleClientRequestInitializer("GooglePlusAPIKey");
但它给出了无法转换 CommonGoogleClientRequestInitializer 的错误

我尝试过: GoogleClientRequestInitializer KEY_INITIALIZER = new CommonGoogleJsonClientRequestInitializer(GooglePlusAPIKey); 但不知道之后该做什么。

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

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

等待回复。

最佳答案

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/

相关文章:

Java:JSON -> Protobuf & 反向转换

java - 在java中暂停音频剪辑

javascript - 如何为accounts.google.com身份验证链接(OAuth)设置X-Frame-Options => ALLOWALL?

java - 当我将其作为jar文件运行时会出现NullPointer异常,但是在Eclipse中可以正常工作

java - 有没有办法使用 TypeAdapter 来序列化父类和子类实例

node.js - Google Search Console API : How do I Solve "User does not have sufficient permission for site"?(当用户有权限时)

node.js - 无法通过 Google API node.js SDK 使用现有访问 token 进行授权

api - 后端错误和检索事件限制

android - 当我从其他计算机上的存储库克隆项目时 Ionic2 出错

parameter-passing - 我可以将哪些其他参数传递给谷歌环聊 uri?