如何使用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);
但我无法在拉队列中插入数据,请帮帮我
我还配置queue.yaml并在此处设置 ACL
结果
{“error”:{“errors”:[{“domain”:“global”,“reason”:“required”, “message”:“需要登录”,“locationType”:“标题”,“位置”: “授权”},“代码”:401,“消息”:“需要登录”}}
答案 0 :(得分:1)
根据你的其他票,请给它一个旋转:
https://github.com/tomwalder/php-appengine-pull-queue
在App Engine上从PHP中删除对REST API的需求。
汤姆
答案 1 :(得分:0)
TL; DR - 您不需要使用CURL手动拨打HTTP电话,您应该使用Google提供的API Client Library,因为它会为您处理所有身份验证内容。
任务队列API(以及大多数Google Cloud Platform API)使用的授权类型是OAuth2。这需要发送“授权:持票人”#39;带有请求的标头,而不是像您一样发送服务器密钥和查询字符串。从您链接到的相同文档中,您可以使用“尝试它!”#39;用于查看OAuth2的完整HTTP请求的部分。
但是,与Google API交互的推荐方法是使用提供的API Client Library for PHP来处理所有授权内容。如果您想了解完整的详细信息,可以在Using OAuth 2.0 to Access Google APIs上阅读,其中包含使用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...)