【问题标题】:URL not found when running sample code运行示例代码时找不到 URL
【发布时间】:2017-05-27 11:31:04
【问题描述】:

我正在尝试运行从 Google 获取的示例,以查看是否可以从 App Engine 访问 Google+。我将以下文件添加到我的项目中并设置 servlet 映射,以便它们在预期的路径上可用。我将它部署到应用程序引擎并尝试访问该应用程序。转到https://mydomain.appspot.com/ 给我错误:NOT_FOUND。访问 servlet 的 url 给了我下面提到的例外情况。样本取自https://github.com/google/google-api-java-client-samples/tree/master/plus-appengine-sample

这些示例可能不是最新的,但它们是我可以从 Google 找到的最新示例。任何关于我应该做些什么来完成这项工作的线索都会有所帮助。

我的课程如下所示:

基本Servlet:

public class PlusBasicServlet extends HttpServlet {

    /**
     * Enter your API key here from https://code.google.com/apis/console/?api=plus under "API Access".
     */
    private static final String API_KEY = "AIzaSyB9NEc2yQRisoj-rIqgg35yeZXReASMRCI";

    private static final long serialVersionUID = 1;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpTransport httpTransport = new UrlFetchTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        Plus plus = new Plus.Builder(httpTransport, jsonFactory, null).setApplicationName("")
                .setGoogleClientRequestInitializer(new PlusRequestInitializer(API_KEY)).build();

        ActivityFeed myActivityFeed = plus.activities().search("Google").execute();
        List<Activity> myActivities = myActivityFeed.getItems();

        resp.setContentType("text/html");
        resp.setStatus(200);
        Writer writer = resp.getWriter();
        writer.write("<ul>");
        for (Activity a : myActivities) {
            writer.write("<li>" + a.getTitle() + "</li>");
        }
        writer.write("</ul>");
    }

}

实用程序:

class Utils {

    /**
     * Global instance of the {@link DataStoreFactory}. The best practice is to make it a single
     * globally shared instance across your application.
     */
    private static final AppEngineDataStoreFactory DATA_STORE_FACTORY =
            AppEngineDataStoreFactory.getDefaultInstance();

    private static GoogleClientSecrets clientSecrets = null;
    private static final Set<String> SCOPES = Collections.singleton(PlusScopes.PLUS_ME);
    static final String MAIN_SERVLET_PATH = "/plussampleservlet";
    static final String AUTH_CALLBACK_SERVLET_PATH = "/oauth2callback";
    static final UrlFetchTransport HTTP_TRANSPORT = new UrlFetchTransport();
    static final JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    private static GoogleClientSecrets getClientSecrets() throws IOException {
        if (clientSecrets == null) {
            clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                    new InputStreamReader(Utils.class.getResourceAsStream("/plus_secret.json")));
            Preconditions.checkArgument(!clientSecrets.getDetails().getClientId().startsWith("Enter ")
                            && !clientSecrets.getDetails().getClientSecret().startsWith("Enter "),
                    "Download client_secrets.json file from https://code.google.com/apis/console/?api=plus "
                            + "into plus-appengine-sample/src/main/resources/client_secrets.json");
        }
        return clientSecrets;
    }

    static GoogleAuthorizationCodeFlow initializeFlow() throws IOException {
        return new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, getClientSecrets(), SCOPES).setDataStoreFactory(
                DATA_STORE_FACTORY).setAccessType("offline").build();
    }

    static String getRedirectUri(HttpServletRequest req) {
        GenericUrl requestUrl = new GenericUrl(req.getRequestURL().toString());
        requestUrl.setRawPath(AUTH_CALLBACK_SERVLET_PATH);
        return requestUrl.build();
    }
}

访问基本 servlet 时出现异常:

Uncaught exception from servlet
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403
{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "The request did not specify any referer. Please ensure that the client is sending referer or use the API Console to remove the referer restrictions.",
    "reason" : "ipRefererBlocked",
    "extendedHelp" : "https://console.developers.google.com/apis/credentials?project=602263912930"
  } ],
  "message" : "The request did not specify any referer. Please ensure that the client is sending referer or use the API Console to remove the referer restrictions."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    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:321)

【问题讨论】:

  • 请记住 stack 不是论坛。通过删除我帮助您修复的错误消息,您现在已经更改了问题,遇到该问题的任何人都将不再看到它。我已回滚您的编辑,以便其他人可以得到帮助

标签: java google-app-engine google-api google-plus google-api-java-client


【解决方案1】:
{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "The request did not specify any referer. Please ensure that the client is sending referer or use the API Console to remove the referer restrictions.",
    "reason" : "ipRefererBlocked",
    "extendedHelp" : "https://console.developers.google.com/apis/credentials?project=602263912930"
  } ],
  "message" : "The request did not specify any referer. Please ensure that the client is sending referer or use the API Console to remove the referer restrictions."
}

当您在 Google Developer 控制台上创建 api 密钥时,您添加了 IP 限制或域限制。您的请求来自您提供的限制以外的其他地方。要么删除限制,要么添加正确的 i 格式。

请注意,实际文档页面的代码应该是最新的。 Activites.search 向下滚动到示例。

【讨论】:

  • 谢谢。现在这个问题没有了。然而,第二个仍然存在。我认为我应该能够浏览到 sampleservlet 的链接,并且它会引导我通过 Google+ 登录。知道为什么我会出错吗?
  • 抱歉,我没有使用 servlet 的经验。您可能希望将其拆分为一个单独的问题。
  • 我会的。感谢您的帮助。
  • 任何时候都记住,如果可以的话,最好将问题保持在一个主题上,尽可能简单。祝你好运,记得接受这个答案
猜你喜欢
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 2017-01-26
相关资源
最近更新 更多