Aammad Ullah
Aammad Ullah

Reputation: 272

Php curl post request to pinterest not working

Changing the code to an access token gives this error:

Error: { "status": 403, "message": "Forbidden" }

This is my code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);

  curl_setopt($ch,CURLOPT_POSTFIELDS,"grant_type=authorization_code&client_id=4853355452345362719&client_secret=deb78310995ec1cf00918a5e688e2148e6043bd640ab16f0f7ecd7543b4ac764&code=".$code);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result =curl_exec ($ch);
curl_close($ch);

print_r($result);

Upvotes: 0

Views: 823

Answers (2)

woodchuck
woodchuck

Reputation: 303

First, you need to generate an access token. This can be done CURLing the following URL: "https://api.pinterest.com/oauth?response_type=code&redirect_uri={$callBackUrl}&client_id={$clientId}&scope=read_public,write_public"

This will return an authorization code, you can then use this to generate your access token using this URL: https://api.pinterest.com/v1/oauth/token with these parameters.

$url = "https://api.pinterest.com/v1/oauth/token";
$body = "grant_type=authorization_code&client_id={$clientId}&client_secret={$clientSecret}&code={$code}";
$ch = curl_init();

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$content = curl_exec($ch);
curl_close($ch);
$data = json_decode($content, true);

Upvotes: 1

Paul Sandel
Paul Sandel

Reputation: 235

403 Forbidden usually suggests a permissions error. I'd bet the remote API does not recognize the code you're sending

Upvotes: 0

Related Questions