Upload API v2 PHP CURL

smugphosmugpho Registered Users Posts: 1 Beginner grinner
Hello,

I'm trying to upload images using the v2 API but i can't seem to get it to work.
Can anyone see whats going wrong?

[PHP]
include_once("oauth/OAuth.php");
include_once("constants.php");

$access_token = '....';
$access_token_secret = '.....';

$consumer = new OAuthConsumer(API_KEY, API_KEY_SECRET);
$signature_method = new OAuthSignatureMethod_PLAINTEXT;

$token = new OAuthToken($access_token, $access_token_secret);

$req_token = OAuthRequest::from_consumer_and_token($consumer, $token, "POST", 'http://upload.smugmug.com');
$req_token->sign_request($signature_method, $consumer, $token);
$parameters = $req_token->get_parameters();

$header[] = 'Authorization: OAuth realm="http://upload.smugmug.com/",
oauth_consumer_key='.$parameters.',
oauth_token='.$parameters.',
oauth_signature_method='.$parameters.',
oauth_signature='.$parameters.',
oauth_timestamp='.time().',
oauth_nonce='.md5(time() . mt_rand()).',
oauth_version=1.0';


$path = 'barcode.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
finfo_close($finfo);

$body[] = 'Accept: application/json';
$body[] = 'X-Smug-Version: v2';
$body[] = 'X-Smug-ResponseType: JSON';
$body[] = 'X-Smug-AlbumUri: /api/v2/album/******';
$body[] = 'X-Smug-Filename: iamge.png';
$body[] = 'Content-MD5: '.$base64.'';
$body[] = 'Content-Length: '.filesize($path).'';
$body[] = 'Content-Type: '.$mime.'';





$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://upload.smugmug.com/');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_error ($ch);



$result = curl_exec($ch);

print_r($result);
[/PHP]

Comments

  • ktvoelkerktvoelker Registered Users Posts: 25 Big grins
    edited July 13, 2015
    The nonce and timestamp are used to calculate the signature, so the call to $req_token->sign_request must be internally generating a nonce and timestamp. When you then generate your own nonce and timestamp to put into the Authorization header, they won't match the ones that were used to calculate the signature. If you consult the documentation for whatever library you are using, you will hopefully find a way to get the nonce and timestamp from $req_token.
    Karl Voelker
    Sorcerer and API Guy at SmugMug
  • ktvoelkerktvoelker Registered Users Posts: 25 Big grins
    edited July 13, 2015
    Also, the Content-MD5 header should be the base-64 encoding of the MD5 digest of the file. What you are sending is a data URL containing the base-64 encoding of the file's entire contents.

    Since that header is optional, I would suggest that you first try removing it.
    Karl Voelker
    Sorcerer and API Guy at SmugMug
Sign In or Register to comment.