使用街景发布API上传全景图像

时间:2017-05-20 11:02:26

标签: google-maps-api-3 google-street-view google-streetview-publish

使用指定的街景API进行上传时

Request an Upload URL
        $ curl --request POST \
        --url 'https://streetviewpublish.googleapis.com/v1/photo:startUpload?key=YOUR_API_KEY' \
        --header 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
        --header 'Content-Length: 0'

Upload the photo bytes to the Upload URL
        $ curl --request POST \
        --url 'UPLOAD_URL' \
        --upload-file 'PATH_TO_FILE' \
        --header 'Authorization: Bearer YOUR_ACCESS_TOKEN'

但它没有工作..

它给我错误请求中找不到文件。

任何人都可以帮我解决这个问题。

先谢谢你。

2 个答案:

答案 0 :(得分:3)

获取我通过这种方式解决的上传网址

$cur_upload_url = curl_init();

  curl_setopt_array($cur_upload_url, array(

   CURLOPT_URL => "https://streetviewpublish.googleapis.com/v1/photo:startUpload?key=XXXXXXXXXXX" ,
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => "" ,
   CURLOPT_CUSTOMREQUEST => "POST",
   CURLOPT_HTTPHEADER => array(
     "authorization: Bearer $access_token",
        "content-type: application/json",
        "Content-Length: 0"
        ), 
  ));

  $response = curl_exec($cur_upload_url);
  $re = '/https?:\/\/[^"]*/';
  $str = $response;
  preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0);
  $upload_url = $_SESSION['UploadRef'] = $matches[0][0];

  echo $upload_url;

将照片字节上传到上传网址

$cmd = exec('curl --request POST \--url "'. addslashes($upload_url) .'" \--upload-file "'.$imagePath.'" \--header "Authorization: Bearer '. addslashes($access_token) .'" ');

然后Uplaod照片的元数据

$curl_meta = curl_init();

  curl_setopt_array($curl_meta, array(
    CURLOPT_URL => "https://streetviewpublish.googleapis.com/v1/photo?key=XXXXXXXXXXXXXXXX",
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => '{
                    "uploadReference":
                    {
                      "uploadUrl": "'.$upload_url.'"
                    },
                    "pose":
                     {
                       "heading": 95.0,
                       "latLngPair":
                       {
                         "latitude": '.$latVal.',
                         "longitude": '.$langVal.'
                       }
                    },
                    "captureTime":
                    {
                      "seconds":  '.$time_stamp.'
                    },
                  }',

    CURLOPT_HTTPHEADER => array(
       "authorization: Bearer $access_token",
       "content-type: application/json"
    ),

  ));


      $response_meta = curl_exec($curl_meta);
      $response_json = json_decode($response_meta, true);
      // $photoID = $response_json['photoId']['id'];
      // echo $photoID;


      if(curl_errno($curl_meta)){
        // this would be your first hint that something went wrong
        die('Couldn\'t send request: ' . curl_error($curl_meta));
      }else{
        //check the HTTP status code of the request.
        $resultStatus = curl_getinfo($curl_meta, CURLINFO_HTTP_CODE);
        if($resultStatus != 200){
          die('Request failed: HTTP status code: ' . $resultStatus);
        }else{
          $photoID = $response_json['photoId']['id'];
          //insert google publish information into db table

        }

      }



      curl_close($curl_meta);

从这里您将获得带照片的身份证。

获得Photo iD发布成功后。

答案 1 :(得分:0)

如果有人希望通过node.js服务器进行上传:

import { exec } from 'child_process';
await new Promise((resolve, reject) => {
  const uploadCmd = `curl --request POST \ --url "${uploadUrl}" \ --upload-file "${__dirname}/360.jpg" \ --header "Authorization: Bearer ${accessToken}" `;
  exec(uploadCmd, (err, stdout, stderr) => {
    if (err) {
      // node couldn't execute the command
      console.log('err', err);
      reject(err);
      return false;
    }
    resolve({stdout, stderr});
    console.log(`Succesfully uploaded: 
      stdout: ${stdout}
      stderr: ${stderr}
    `);
  });
});

不要忘记在await函数中执行async部分。