pinterest / api-quickstart

Code that makes it easy to get started with the Pinterest API.
Apache License 2.0
125 stars 46 forks source link

Are there plans to provide an PHP example? #82

Closed CreativeNative closed 11 months ago

CreativeNative commented 1 year ago

I would really appreciate an example for a quick start in php => 8.0

davidchaiken commented 1 year ago

Thanks for the feedback. Is there a specific endpoint or flow that you would like to see in PHP?

CreativeNative commented 1 year ago

@davidchaiken Thank you for your patience. I would like to see an example for

I think there are many developers who would love to see an example. From that point, we could explore the new API on our own. Many thanks in advance.

davidchaiken commented 1 year ago

Thanks for this information! It's helpful for scoping the work required.

PHP has been on my wish list for a while. No promises, but I'll chat with folks inside Pinterest to see if anyone can write the PHP code quickly. (I haven't coded PHP myself for a number of years.) In the meantime, I'd be happy to look at a PHP pull request for this repo or maybe a remix of the Pinterest OAuth tutorial on Glitch.

Upvotes on this issue (+1 comments) would also help. :)

CreativeNative commented 1 year ago

@davidchaiken Why did you close this issue? Do you not want to provide an example in PHP?

davidchaiken commented 1 year ago

I suppose I can keep it open based on your interest.

However, this issue is not getting upvotes and I haven't found anyone at Pinterest who has the time & inclination to write PHP code yet.

CreativeNative commented 1 year ago

Of course there aren't so many upvotes. This repository has 7 "watches" and your company has 270 followers. Only to compare, Meta has 10K. But I believe there is a great need for an PHP example. Isn't PHP the most commonly used server-side programming language for building websites? For example, Twitter wants to close his free API access. So, where do you think all these people will go? Would be a great opportunity now, wouldn't it?

That you don't find a person with time & inclination to write a PHP example only reinforces my opinion that it is more complicated than I thought.

Actually, I thought that issue should be in your interest to spread the use of the new API, but if you don't have the inclination, you can also close the issue. It's up to you.

davidchaiken commented 1 year ago

Thanks for the great feedback! You're right. I'll keep this issue open. :)

ickata commented 1 year ago

I really can't believe that Pinterest dev/support team are so unresponsive regarding php support! I do not like php too, but there are many products built on top of it.

Look, guys. You really have to take this one seriously. I am trying to execute a successful access token request for weeks now, and I cannot. I am using php's curl_exec and I am strictly following your API documentation example (in bash). I do everything as documented; however, I keep getting

{"code":2,"message":"Authentication failed.","status":"failure"}

when I try to exchange my successfully retrieved authorization code before, for a token.

My code:

        $auth_url = "https://api.pinterest.com/v5/oauth/token/";
        $code = $_GET['code'];
        $client_id = '...';
        $client_secret = '...';
        $authorization = base64_encode($client_id . ':' . $client_secret);
        $redirect_uri = . 'https://mydomain.com/pinterest';
        echo $code . "\n" . $authorization . "\n" . $redirect_uri . "\n";

        $headers = array(
            "Content-Type: application/x-www-form-urlencoded",
            'Authorization: Basic ' . $authorization
        );
        $params = array(
            "grant_type" => "authorization_code",
            "code"        => $code,
            "redirect_uri" => $redirect_uri,
            "state" => $_GET['state'],
        );
        $data = http_build_query($params);

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $auth_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => $headers
        ));
        $result = curl_exec($curl);
        curl_close($curl);

I am sure that I do not have problems with permissions because I can successfully exchange the authorization code for a token in a bash curl request. But php fails every time with the error above. Client ID/Secret are correct. Everything is set correctly.

I've contacted the Support team hundred times, but they apologise every time saying that you don’t have official php examples in the docs, hence they cannot provide support.

Please, take this seriously, and provide us with a working example, or fix the issues in your API. I have strong reasons to believe that your API rejects the php request for one reason or another, and this must be fixed in your API...

davidchaiken commented 1 year ago

Hello! Sorry for the frustration. Your question and code sample above really helps!

I got your code working on my MacBook using PHP 7.4.27. I needed to fix just a couple of things...

First, the auth_url doesn't have a slash at the end:

replace: $auth_url = "https://api.pinterest.com/v5/oauth/token/"; with: $auth_url = "https://api.pinterest.com/v5/oauth/token";

Second, according to the docs on curl_setopt:

CURLOPT_POSTFIELDS is the full data to post in a HTTP "POST" operation. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

This is the modification of your code that worked for me:

$postfields = "grant_type=authorization_code&code=" . $code . "&redirect_uri=" . $redirect_uri; curl_setopt_array($curl, array(... ... CURLOPT_POSTFIELDS => $postfields, CURLOPT_POSTFIELDS => $headers

With these two changes, curl worked. I also noticed that the "Content-Type: application/x-www-form-urlencoded" header is redundant with the automatic header generation by curl. Note that I skipped the state parameter just for this test, but the state parameter is a best practice for production code.

I hope that this information is what you need to successfully generate a token!

ickata commented 1 year ago

Hey @davidchaiken ,

Many thanks for your quick response! Removing the last slash from the auth url helped! I really can't believe that this / was the problem. Now it works even w/ the x-form-urlencoded header. I know it's redundant, but believe me, I tried also many other things trying to beat this unknown problem.

Still, IMHO your API does not return the proper error in this case. The returned error is "Authentication failed." while it should be 400 Bad Request or 404 Not Found.

Thank you!

davidchaiken commented 1 year ago

Thanks for your feedback, @ickata. I verified your observation in your last comment. The only thing that needed to be changed was removing the / at the end of the auth_url. I agree that the error needs to be improved, and will file a bug report on this issue. The Pinterest Developer Platform team has been spending a lot of time ensuring that the functionality of v5 is suitable for migrating all of our developers from previous versions of the API. Once they get v5 to a good point from the point of view of functionality, I expect that they'll circle back to fix a bunch of problems in the area of error reporting. (I've already filed a few other bug reports in this area.)

Best wishes for successful development with the Pinterest API!

davidchaiken commented 11 months ago

First draft: https://github.com/pinterest/api-quickstart/compare/dc_PINDP-2404

Comments welcome.

davidchaiken commented 11 months ago

PR merged.