【问题标题】:How to create a Bearer token for GOOGLE CLOUD JSON API如何为 GOOGLE CLOUD JSON API 创建不记名令牌
【发布时间】:2018-05-21 08:19:56
【问题描述】:

要将对象上传到谷歌云存储桶,我需要一个身份验证令牌。我选择了 JSON API 方法将对象上传到存储桶。

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,  "https://www.googleapis.com/upload/storage/v1/b/Mybucket/o?uploadType=media");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "yourdata");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
$headers[] = "Authorization:Bearer <TOKEN>";
$headers[] = "x-goog-project-id: xxxxxxxxxxx";
$headers[] = "x-goog-user-project: xxxxxxxxxxx";
$headers[] = "Content-Length: 8";
$headers[] = "Content-Type: image/jpeg";
$headers[] = "x-goog-acl: public-read-write";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
  1. 有没有办法上传没有这些类型的对象 身份验证或任何其他身份验证方法?
  2. 如何使用 api/curl/php 以编程方式生成此令牌(承载)?

【问题讨论】:

  • 您是否检查过this document,其中说明了云存储的身份验证方法,这可能对这种情况有所帮助。

标签: google-cloud-storage bearer-token bucket


【解决方案1】:
function getOAuthToken(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    $headers = array();
    $headers[] = "Metadata-Flavor: Google";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
      //  echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    $result = json_decode($result,true);
    return $result['access_token'];
}

$result['access_token'] 是不记名令牌。

【讨论】:

    猜你喜欢
    • 2014-11-20
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2016-09-12
    • 2014-06-10
    • 1970-01-01
    • 2019-09-27
    相关资源
    最近更新 更多