App Engine 上的 Cronjob 导致方法不允许错误 (405)

Cronjob on App Engine results in a Method Not Allowed error (405)

我正在尝试 运行 在带有端点的 App Engine 上 cronjob

我在使用 cronjob 时收到不允许的方法 (HTTP 405) 错误。我怎样才能解决这个错误?

这是我现在的 cron.xml:

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
    <cron>
        <url>/_ah/spi/messaging/v1/checkUpdates</url>
        <description>Check for news every 5 minutes</description>
        <schedule>every 5 minutes</schedule>
    </cron>
    <cron>
        <url>/_ah/spi/package.backend.MessagingEndpoint.checkUpdates</url>
        <description>Check for news every 5 minutes</description>
        <schedule>every 5 minutes</schedule>
    </cron>
</cronentries>

没有代码主体的端点:

@ApiMethod(
        name = "checkUpdatesPost",
        httpMethod = ApiMethod.HttpMethod.POST
)
public void checkUpdatesPost() {
    checkUpdates();
}

@ApiMethod(
        name = "checkUpdates",
        httpMethod = ApiMethod.HttpMethod.GET
)
public void checkUpdates() {
    // ... Stuff
}

运行 正确地通过 Google APIs Explorer 运行s 的这些函数中的任何一个:

使用 GET

进行测试
GET https://myapp.appspot.com/_ah/api/messaging/v1/checkUpdates

使用 POST

进行测试
POST https://myapp.appspot.com/_ah/api/messaging/v1/checkUpdatesPost

这是日志,如 Logs Viewer 上所见:

可以看到这里AppEngine-.指的是Cron的一次尝试。 Chrome 48 是我的浏览器/Google APIs Explorer 的尝试。

据我了解,Cron 不能POST 一起使用。为此,我为 GET 创建了另一个函数,它反映了 POST.

的函数

在我的 cron.xml 中,我提供了两种变体。

  1. /_ah/spi/messaging/v1/checkUpdates - 这就是我在浏览器中用来访问资源的方式。
  2. /_ah/spi/package.backend.MessagingEndpoint.checkUpdates - 这是我在浏览器中拥有 运行 #1 后在日志查看器中看到的内容。

好像在documentation里说得很清楚了。

Calling Google Cloud Endpoints

You cannot call a Google Cloud Endpoint from a cron job. Instead, you should issue a request to a target that is served by a handler that's specified in your app's configuration file or in a dispatch file. That handler then calls the appropriate endpoint class and method.