首页 文章

使用google plus Api在google stream上公开发布帖子[关闭]

提问于
浏览
0
showing error response

    {
      "code" : 403,
      "errors" : [ {
        "domain" : "plusDomains",
        "message" : "The operation is not allowed because the activity is not domain restricted.",
        "reason" : "notDomainRestricted"
      } ],
      "message" : "The operation is not allowed because the activity is not domain restricted."
    }

//this is complete code to share a post google stream //Using google plus write stream

package com.org.test;导入com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson.JacksonFactory; import com.google.api.services.plusDomains.PlusDomains; import com.google.api.services.plusDomains.model.Acl; import com.google.api.services.plusDomains.model.Activity; import com.google.api.services.plusDomains.model.Circle; import com.google.api.services.plusDomains.PlusDomains.Circles.AddPeople; import com.google.api.services.plusDomains.model.PlusDomainsAclentryResource; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; / ** *用于演示Google Domains API的简单程序 . * 此程序显示如何为域范围的委派验证应用程序以及如何完成activities.insert API调用 . 有关如何使用OAuth 2.0对每个用户进行身份验证的详细信息,或其他API调用的示例,请参阅https://developers.google.com/+/domains/上的文档 . * * @author joannasmith@google.com(Joanna Smith)/公共类DomainDelegation {/ * *更新SERVICE_ACCOUNT_EMAIL,其中包含在开发者控制台中创建的客户ID *的服务帐户的电子邮件地址 . * / private static final String SERVICE_ACCOUNT_EMAIL = "xxxxxx738373-q7b1r8unnf57qnpffjgku78xxxxxxxxx@developer.gserviceaccount.com";

/**
   * Update SERVICE_ACCOUNT_PKCS12_FILE_PATH with the file path to the private key file downloaded
   *  from the developer console.
   */
  private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH =
      "D:\\new\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12";

  /**
   * Update USER_EMAIL with the email address of the user within your domain that you would like
   *  to act on behalf of.
   */
  private static final String USER_EMAIL = "admin@xxxxxxxxxxxxxxxxxxx.co.in";


  /**
   * plus.me and plus.stream.write are the scopes required to perform the tasks in this quickstart.
   *  For a full list of available scopes and their uses, please see the documentation.
   */
  private static final List<String> SCOPE = Arrays.asList(
          "https://www.googleapis.com/auth/plus.me",
          "https://www.googleapis.com/auth/plus.stream.write",
          "https://www.googleapis.com/auth/plus.circles.read",
            "https://www.googleapis.com/auth/plus.circles.write");


  /**
   * Builds and returns a Plus service object authorized with the service accounts
   * that act on behalf of the given user.
   *
   * @return Plus service object that is ready to make requests.
   * @throws GeneralSecurityException if authentication fails.
   * @throws IOException if authentication fails.
   */
  private static PlusDomains authenticate() throws GeneralSecurityException, IOException {

    System.out.println(String.format("Authenticate the domain for %s", USER_EMAIL));

    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    // Setting the sub field with USER_EMAIL allows you to make API calls using the special keyword 
    // 'me' in place of a user id for that user.
    GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(jsonFactory)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountScopes(SCOPE)
        .setServiceAccountUser(USER_EMAIL)
        .setServiceAccountPrivateKeyFromP12File(
            new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
        .build();

    // Create and return the Plus service object
    PlusDomains service = new PlusDomains.Builder(httpTransport,
            jsonFactory, credential).setApplicationName("Circle")
            .setRootUrl("https://www.googleapis.com/")
            .setHttpRequestInitializer(credential).build();
    return service;
  }

public static void main(String[] args) throws Exception {
// Create an authorized API client
    PlusDomains service = authenticate();

    // Set the user's ID to 'me': requires the plus.me scope
    String userId = "me";
    String msg = "Last Post Today Work on it next day";

    System.out.println("Inserting activity");

    // Create the audience of the post
    PlusDomainsAclentryResource res = new PlusDomainsAclentryResource();
    // Share to the domain
    res.setType("domain");

    List<PlusDomainsAclentryResource> aclEntries = new ArrayList<PlusDomainsAclentryResource>();
    aclEntries.add(res);

    Acl acl = new Acl();
    acl.setItems(aclEntries);
    // Required, this does the domain restriction
    acl.setDomainRestricted(false);

    Activity activity = new Activity()
        .setObject(new Activity.PlusDomainsObject().setOriginalContent(msg))
        .setAccess(acl);

    activity = service.activities().insert(userId, activity).execute();

    System.out.println(activity);
  }
}

1 回答

  • 3

    目前,您只能为Google Domains执行此操作,这是一种内部空间,例如,您的组织 . 它不是免费的(如果我没记错的话,可以进行1个月的试用)并且所有的活动都不公开 . 在我看来,个人发展是如此无用 .

相关问题