Google + Domains API Java快速入门不起作用 [英] Google + Domains API Quick start for java is not working

查看:148
本文介绍了Google + Domains API Java快速入门不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google +提供的域API,我正在尝试使用域名范围的委派使Java快速启动。我遵循了这些步骤,还要求我的域管理员授予访问权限项目我在控制台中创建,恢复我可以编译java文件,但是当我运行时,我得到一个404错误,这是代码:

  / * 
*版权所有2013 Google Inc.保留所有权利。
*
*根据Apache许可证2.0版许可(许可证);
*除遵守许可证外,您不得使用此文件。
*您可以在
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*中获得许可证副本除非根据适用法律要求或书面同意,根据许可分配的软件
*以原样基础,
*分发,不附有任何明示或暗示的担保或条件。
*请参阅许可证以了解许可证下特定语言的管理权限和
*限制。
* /

包com.google.plus.samples.quickstart.domains;

导入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;
导入com.google.api.services.plusDomains.model.Acl;
导入com.google.api.services.plusDomains.model.Activity;
导入com.google.api.services.plusDomains.model.PlusDomainsAclentryResource;
import com.google.api.services.plusDomains.model.Person;

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/+上的文档/域/。
*
* @author joannasmith@google.com(Joanna Smith)
* /
public class DomainDelegation {
/ **
*更新SERVICE_ACCOUNT_EMAIL在开发者控制台中创建的客户端ID
*的服务帐户的电子邮件地址。
* /
private static final String SERVICE_ACCOUNT_EMAIL =example@developer.gserviceaccount.com;
$ b $ / **
*将SERVICE_ACCOUNT_PKCS12_FILE_PATH更新为从开发者控制台下载的私钥文件的文件路径
*。
* /
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH =
file-privatekey.p12;

/ **
*更新USER_EMAIL,您的域名中包含用户的电子邮件地址,您希望
*代表您的行为。
* /
private static final String USER_EMAIL =example@email.com;


/ **
* plus.me和plus.stream.write是在本快速入门中执行任务所需的范围。
*有关可用范围及其用途的完整列表,请参阅文档。
* /
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.profiles.read,
https://www.googleapis.com/auth/plus.stream.read,
https://www.googleapis.com/auth/userinfo.profile);


/ **
*构建并返回授权服务帐户
*的Plus服务对象,该服务帐户代表给定用户。
*
* @return Plus服务对象已准备好发出请求。
*如果认证失败,则抛出GeneralSecurityException。
* @如果认证失败,则抛出IOException。
* /
private static PlusDomain authenticate()抛出GeneralSecurityException,IOException {

System.out.println(String.format(验证%s的域名,USER_EMAIL) );

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

//使用USER_EMAIL设置子字段允许您使用特殊关键字
//'me'进行API调用,以代替该用户的用户标识。
GoogleCredential凭证=新的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();

System.out.println(凭证+凭证);
//创建并返回Plus服务对象
PlusDomains服务= new PlusDomains.Builder(httpTransport,jsonFactory,凭证).build();

退货服务;
}

/ **
*代表与该服务的凭证对象关联的用户创建一个新帖子,
*仅限于该域。
*
* @param service Plus服务对象已准备好发出请求。
* @如果插入操作失败或认证失败,则抛出IOException。
*如果认证失败,则抛出GeneralSecurityException。
* /
public static void main(String [] args)throws Exception {
//创建授权的API客户端
PlusDomains service = authenticate();

//将用户的ID设置为'me':需要加上.me作用域
String userId =me;
String msg =Happy Monday!#caseofthemondays;

System.out.println(插入活动+服务);

//创建帖子的读者
PlusDomainsAclentryResource res = new PlusDomainsAclentryResource();

//分享到域
res.setType(domain);


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

Acl acl = new Acl();
acl.setItems(aclEntries);

//必需,这是域限制
acl.setDomainRestricted(true);

Activity activity = new Activity()
.setObject(new Activity.PlusDomainsObject()。setOriginalContent(msg))
.setAccess(acl);
//System.out.println(\"ativity+ activity);

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

System.out.println(activity);






隐含的电子邮件和密钥文件等数据我拥有在我的代码中正确,这是我得到的错误:

 验证域名为example@email.com 
凭证com.google.api.client.googleapis.auth.oauth2.GoogleCredential@2b275d39
04-dic-2013 8:59:50 com.google.api.client.googleapis.services.AbstractGoogleClient< init>
ADVERTENCIA:应用程序名称未设置。调用Builder#setApplicationName。
插入活动com.google.api.services.plusDomains.PlusDomains@46b8c8e6
线程main中的异常com.google.api.client.googleapis.json.GoogleJsonResponseException:404未找到
在com.google.api.client.googleapis.json.GoogleJsonResponseException.from上找不到
(GoogleJsonResponseException.java:145)
在com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest。 newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
,com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
,位于com.google.api.client。 googleapis.services.AbstractGoogleClientRequest $ 1.interceptResponse(AbstractGoogleClientRequest.java:312)
在com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1045)
在com.google.api .client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
通过com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.google.plus.samples.quickstart.domains.DomainDelegation.main(DomainDelegation.java:154)

我迷路了,如果有人能帮助我,我会很感激。

包含PlusDomains.builder的行现在需要调用setApplicationName:

  PlusDomains service = new PlusDomains.Builder(httpTransport,jsonFactory,凭证)
.setApplicationName('MyDomainsDemo')
.build();


I am trying to use the domains API offered in Google +, I am trying to make work to Quick start for java using domain-wide delegation I have followed the steps, also I have asked my domain admin to grant access to the project I created in console, resuming I can compile the java file, but when I run, I get a 404 error, here is the code:

 /*
 * Copyright 2013 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.plus.samples.quickstart.domains;

import 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.PlusDomainsAclentryResource;
import com.google.api.services.plusDomains.model.Person;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Simple program to demonstrate the Google+ Domains API.
 *
 * This program shows how to authenticate an app for domain-wide delegation and how
 * to complete an activities.insert API call. For details on how to authenticate on
 * a per-user basis using OAuth 2.0, or for examples of other API calls, please see
 * the documentation at https://developers.google.com/+/domains/.
 *
 * @author joannasmith@google.com (Joanna Smith)
 */
public class DomainDelegation {
  /**
   * Update SERVICE_ACCOUNT_EMAIL with the email address of the service account for the client ID
   *  created in the developer console.
   */
  private static final String SERVICE_ACCOUNT_EMAIL = "example@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 =
      "file-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 = "example@email.com";


  /**
   * 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.profiles.read",
      "https://www.googleapis.com/auth/plus.stream.read",
      "https://www.googleapis.com/auth/userinfo.profile");


  /**
   * 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();

    System.out.println("credential " + credential);
    // Create and return the Plus service object
    PlusDomains service = new PlusDomains.Builder(httpTransport, jsonFactory, credential).build();

    return service;
  }

  /**
   * Create a new post on behalf of the user associated with the credential object of the service,
   * restricted to the domain.
   *
   * @param service Plus service object that is ready to make requests.
   * @throws IOException if the insert operation fails or if authentication fails.
   * @throws GeneralSecurityException if authentication fails.
   */
  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 = "Happy Monday! #caseofthemondays";

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

    // 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(true);

    Activity activity = new Activity()
        .setObject(new Activity.PlusDomainsObject().setOriginalContent(msg))
        .setAccess(acl);
    //System.out.println("ativity " + activity);

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

    System.out.println(activity);
  }
}

Obviusly the data like email and key file I have the correct in my code, this is the error I get:

Authenticate the domain for example@email.com
credential com.google.api.client.googleapis.auth.oauth2.GoogleCredential@2b275d39
04-dic-2013 8:59:50 com.google.api.client.googleapis.services.AbstractGoogleClient <init>
ADVERTENCIA: Application name is not set. Call Builder#setApplicationName.
Inserting activity com.google.api.services.plusDomains.PlusDomains@46b8c8e6
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
Not Found
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1045)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
    at com.google.plus.samples.quickstart.domains.DomainDelegation.main(DomainDelegation.java:154)

I am lost, please if somebody could help me I would be grateful.

解决方案

It appears that the line that contains PlusDomains.builder now requires that the setApplicationName be called:

PlusDomains service = new PlusDomains.Builder(httpTransport, jsonFactory, credential)
  .setApplicationName('MyDomainsDemo')
  .build();

这篇关于Google + Domains API Java快速入门不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆