Yuichi Tsunoda
Yuichi Tsunoda

Reputation: 21

Pinterest API, I tried to exchange the code for an access token, but returned unexpected error code

I tried to get access token via Perl as follows.

    my $ua = Mojo::UserAgent->new;
    my $base64 = encode_base64 "{id}:{secret}";

    my $url = "https://api.pinterest.com/v5/oauth/token";
    my $tx = $ua->post(
        $url => {
            'Authorization' => 'Basic ' . $base64,
            'Content-Type' => 'application/x-www-form-urlencoded'
        } => form => {
            'grant_type' => 'authorization_code',
            'code' => {code},
            'redirect_uri' => {my redirect url}
        }
    ); 

Returned following error.

<HTML><HEAD>
<TITLE>Invalid URL</TITLE>
</HEAD><BODY>
<H1>Invalid URL</H1>
The requested URL "http&#58;&#47;&#47;&#37;5bNo&#37;20Host&#37;5d&#47;v5&#47;oauth&#47;token", is invalid.<p>
Reference&#32;&#35;9&#46;5758c317&#46;1698500314&#46;d8e9663
</BODY></HTML>

Anyone know the reason? What is above URL? "http://%5bNo%20Host%5d/v5/oauth/token"

How to fix this? And I want to know what URL is that? Probably that was encoded.

"http://[No Host]/v5/oauth/token"

==================================================

And I tried that few times with same code. I got different messages as follows. It is very weird...

{"code":1,"message":"Missing request body"}

{"code":1,"message":"Invalid parameters."}

Upvotes: 1

Views: 670

Answers (1)

Yuichi Tsunoda
Yuichi Tsunoda

Reputation: 21

Self solved.

$base64 had unexpected CRLF. When I added chomp($base64), problem solved. Is the encode_base64 will add CRLF automatically? I'll see original code.

Many thanks!

Upvotes: 1

Related Questions