如何在 GAE 上使用 rest API(php 代码)在拉队列中插入数据


How to insert data at pull queue using rest API(php code) at GAE

如何在 GAE 上使用 rest API(php 代码)在拉队列中插入数据

我正在关注以下链接https://cloud.google.com/appengine/docs/python/taskqueue/rest/tasks/insert我的代码是

$target_url = 'https://content.googleapis.com/taskqueue/v1beta2/projects/s~<projectname>/taskqueues/<task_queue_name>/tasks?key=<Server key >&alt=json';
$post = array('queueName' => '<task_queue_name>', 'payloadBase64'=>'aGVsbG8=');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
print_r($server_output);
curl_close ($ch);

但是我无法在拉队列中插入数据,请帮助我

我还配置了队列.yaml并在此处设置ACL

结果

{ "错误": { "错误": [ { "域": "全局", "原因": "必需", "消息": "需要登录", "位置类型": "标头", "位置": "授权" } ], "代码": 401, "消息": "需要登录" } }

根据您的其他票证,请试一试:

https://github.com/tomwalder/php-appengine-pull-queue

从 App Engine 上的 PHP 中移除了对 REST API 的需求。

汤姆

TL;DR - 您不需要使用 CURL 手动进行 HTTP 调用,您应该使用 Google 提供的 API 客户端库,因为它会为您处理所有身份验证工作。

与任务队列 API(以及大多数 Google Cloud Platform API)一起使用的授权类型是 OAuth2。这需要随请求一起发送"授权:持有者"标头,而不是像您那样发送服务器密钥和查询字符串。从您链接到的同一文档中,您可以使用"试用!"部分来查看OAuth2的完整HTTP请求是什么样子的。

但是,与Google API交互的推荐方法是使用提供的PHPAPI客户端库,该库会为您处理所有授权工作。如果您想了解完整的详细信息,可以在使用OAuth 2.0访问Google API中阅读有关它的信息,其中包含使用PHP和原始HTTP的不同类型的应用程序的示例。

以下未经测试的代码片段显示了使用具有 OAuth2 的服务帐户插入客户端库的任务可能如下所示:

$client = new Google_Client();
$client->setScopes(['https://www.googleapis.com/auth/taskqueue']);
$client->setAuthConfig($credentials_file);  // This is your service account JSON key you need to export from the Developers Console
$service = new Google_Service_Taskqueue($client);
$results = $service->insert(opts...)