如何指定OAUTH命令为POST,PUT,Delete

时间:2018-07-02 01:57:26

标签: php oauth-2.0

我正在尝试将文件上传到Shapeways。我已经成功验证自己并收到访问令牌,但是当我尝试上传时失败。我的代码如下。我怀疑问题出在演示代码(https://developers.shapeways.com/getting-started?li=devhome_main#upload-and-sell)上,我无法使用它,因为它使用PECL指定它是POST命令。我怀疑这可能与我的代码不同,因为它们还将PUT和DELETE指定为选项。这是PECL自动添加的另一个字段或标题吗?

<?php   session_start();
    require_once 'config.php';
    /* contains

define('OAUTH2_CLIENT_ID', 'Redacted');
define('OAUTH2_CLIENT_SECRET', 'Redacted');

define('OAUTH2_URL_AUTHORIZE','https://api.shapeways.com/oauth2/authorize');
define('OAUTH2_URL_TOKEN','https://api.shapeways.com/oauth2/token');
define('OAUTH2_URL_BASE', 'https://api.shapeways.com/');

    */


function apiRequest($url, $post=FALSE, $headers=array()) {
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  if($post)
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  $headers[] = 'Accept: application/json';
  if(session('access_token'))
    $headers[] = 'Authorization: Bearer ' . session('access_token');
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $response = curl_exec($ch);
  return json_decode($response);
}

function session($key, $default=NULL) {
  return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
}

    //make sure we are connected to shapeways
    if (!isset($_SESSION['access_token'])) {
        $token=apiRequest(OAUTH2_URL_TOKEN, array(
            "grant_type"=>      "client_credentials",
            "client_id"=>       OAUTH2_CLIENT_ID,
            "client_secret"=>   OAUTH2_CLIENT_SECRET    
        ));
        $_SESSION['access_token'] = $token->access_token;
    }

    //try to upload file
    $filename = "model_j5yNGd"; //does exist
    $file = file_get_contents($filename);
    $data = array(
      "fileName" => $filename.'.zip',
      "file" => rawurlencode(base64_encode($file)),
      "hasRightsToModel" => 1,
      "acceptTermsAndConditions" => 1,

    );
    $response = apiRequest(OAUTH2_URL_BASE ."models/v1", $data,$array);
    print_r($response);
    /*  expecting
    {
      "result":"success",
      "fileName":"????",
      "contentLength":2250906,
      "fileMd5Checksum":"8f8db6c000934a5ec03c701548bb0daf",
      "modelId":????,
      "modelVersion":0
    }
    */

    /*get
    {
        "result":"failure",
        "reason":"Field is required, but missing. Field is required, but missing.",
        "rateLimit_OkToGo_InSeconds": 0,
        "rateLimit_Window_InMinutes": 5,
        "rateLimit_Limit": 150,
        "rateLimit_Remaining": 149,
        "rateLimit_History": {
            "201807020054": 1
        },
        "rateLimit": {....}
    }       
    */

0 个答案:

没有答案