找不到Google+ Domains API 404错误 [英] Google+ Domains API 404 Not Found error

查看:73
本文介绍了找不到Google+ Domains API 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google+ Domains API运行Java的快速入门,并检索404 Not found错误,但我不知道为什么会发生这种情况.我需要此api来为客户预先填充圈子.

I'm trying to run the Quick start for Java with the Google+ Domains API and retrieving a 404 Not found error and I can't figure out why this happens. I need this api for pre-populate circles for a customer.

链接到快速入门指南: https://developers.google.com/+/domains/quickstart/java

Link to quick start guide: https://developers.google.com/+/domains/quickstart/java

我按照说明进行了操作,并且可以进行授权,但是当我想对API进行操作时,我会立即得到404 Not Found.

I followed the instructions and I am able to authorize, but when I want to do something with the API I immediately get 404 Not Found.

因为授权已经完成,所以我期望使用带有私钥文件的服务帐户正确配置API Access.我正在使用域范围的委托,并代表userx@example.com.

Because the authorization is already complete, I expect that the API Access is correct configured using a service account with a private key file. I'm using domain wide delegation and act as behalf of userx@example.com.

代码:

 /*
 * 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.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpResponseException;
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 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 = "acccount@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 =
      "/path/to/xxx-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 = "user@example.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.circles.read",
            "https://www.googleapis.com/auth/plus.circles.write",
            "https://www.googleapis.com/auth/plus.media.upload",
            "https://www.googleapis.com/auth/plus.stream.read",
            "https://www.googleapis.com/auth/plus.stream.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).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 {
    try{
        try {
            // Create an authorized API client
            PlusDomains service = authenticate();
            System.out.println(service.getRootUrl());

            // 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");

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

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

            System.out.println(activity);

    } catch (GoogleJsonResponseException e) {
        // message already includes parsed response
        System.err.println(e.getMessage());
    } catch (HttpResponseException e) {
        // message doesn't include parsed response
        System.err.println(e.getMessage());
        //System.err.println(e.getResponse().parseAsString());
    }
  } catch (Throwable t) {
    t.printStackTrace();
  } 
  }
}

日志消息:

Authenticate the domain for n.waarbroek@dev.caase.com
Nov 27, 2013 2:21:18 PM com.google.api.client.googleapis.services.AbstractGoogleClient         <init>
WARNING: Application name is not set. Call Builder#setApplicationName.
Inserting activity
404 Not Found
Not Found

有人知道为什么我会收到404 Not Found吗?我正在针对企业帐户的免费测试域上运行此脚本.

Does anyone know why I get a 404 Not Found? I'm running this script on a free test domain for enterprise accounts.

推荐答案

  1. 您可以检查是否已在开发人员控制台中启用了Google+ Domains API.

  1. Can you check you have enabled the Google+ Domains API in the developers console.

您是否可以确保从 GitHub -进行了更改,将API从plus/v1domains重命名为plusDomains/v1,如果您使用的是旧版本,可能会遇到麻烦.

Can you make sure you have the latest version of the quickstart from GitHub - there was a change to rename the API to plusDomains/v1 from plus/v1domains which could be hitting you if you have an old build.

您可以检查要测试的域上是否启用了Google +

Can you check Google+ is enabled on the domain you are testing

最后,您能否在问题中包含有关请求和响应的更多详细信息-如果您可以注销并包括这些内容,那可能会有所帮助!

Finally, could you include in your question more detail on the request and response - if you can log them out and include that, that might help!

这篇关于找不到Google+ Domains API 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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