【问题标题】:How to insert data at pull queue using rest API(php code) at GAE如何在 GAE 使用 rest API(php 代码)在拉队列中插入数据
【发布时间】:2016-01-19 06:36:39
【问题描述】:

如何在 GAE 中使用 rest API(php 代码)在 pull queue 中插入数据

我正在关注以下链接 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", “消息”:“需要登录”,“locationType”:“标题”,“位置”: "授权" } ], "code": 401, "message": "需要登录" } }

【问题讨论】:

    标签: php python api rest google-app-engine


    【解决方案1】:

    根据你的其他票,试一试:

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

    在 App Engine 上不再需要 PHP 的 REST API。

    汤姆

    【讨论】:

    • 没问题!如果您有任何问题,请告诉我。
    • 当然——开源!
    • 在文档中有一个注释,即 ALPHA 这个库处于开发的早期阶段。不要在生产中使用它。它会改变。所以我很担心。虽然这个库运行成功
    • 这取决于你。文档是正确的,该库是 alpha 版本。我会开发它,添加功能,让它随着时间的推移变得更加强大。
    • 如果我使用此代码,将来可能会发生任何致命错误??我在生产中使用此代码。所以请告诉我..
    【解决方案2】:

    TL;DR - 您无需使用 CURL 手动进行 HTTP 调用,您应该使用 Google 提供的 API Client Library,因为它会为您处理所有的身份验证内容。

    Task Queue API(以及大多数 Google Cloud Platform API)使用的授权类型是 OAuth2。这需要在请求中发送“Authorization: Bearer”标头,而不是像您正在做的那样发送服务器密钥和查询字符串。在您链接到的同一个文档中,您可以使用“试试看!”部分以查看使用 OAuth2 的完整 HTTP 请求是什么样的。

    但是,与 Google API 交互的推荐方法是使用提供的 API Client Library for PHP,它会为您处理所有授权内容。如果您想了解完整的详细信息,可以在Using OAuth 2.0 to Access Google APIs 阅读有关它的内容,其中包含使用 PHP 和原始 HTTP 的不同类型应用程序的示例。

    以下未经测试的 sn-p 显示了如何使用具有 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...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2022-01-11
      • 2014-08-29
      • 2015-12-13
      • 2020-02-24
      • 1970-01-01
      相关资源
      最近更新 更多