nimmolo nimmolo - 1 month ago 20
PHP Question

Vimeo API - generate unauthenticated token with WP HTTP API

I'm trying to get an unauthenticated token from Vimeo's current API v3, in order to use it to get a simple list of my own videos on my own website. I'm using a WordPress HTTP API function 'wp_remote_post' to generate a proper http request.

According to Vimeo, this is the correct way to do this and it's done with a POST request. Here are the arguments:

HTTP Method: POST
HTTP URL: api.vimeo.com/oauth/authorize/client
HTTP Headers: Authorization: basic, base64_encode( "$client_id: $client_secret" )
Request Body: grant_type=client_credentials&scope=public%20private


enter image description here

and getting

[body] => {"error":"You must provide a valid authenticated access token."}
[raw] => HTTP/1.1 401 Authorization Required


Why is Vimeo asking for a valid authenticated access token on an explicitly UNauthenticated call? I have provided the actual client id and client secret from my Vimeo app, using Vimeo's instructions to receive an unauthenticated access token. I'm sending the request from my local environment.

I have checked the similar question How to generate Vimeo unauthenticated access token? and have followed everything outlined there. No dice, and i've been trying to do this for hours.

Answer

Vimeo API seems to only accept the parameters as part of the query string, not as part of the HTTP POST object ('body', 'data', or other).

Only when I coded the parameters directly into the URL, rather than passing them as parameters in the post object, the post worked.

Works:

$url = 'https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials&scope=public%20private';
$auth = base64_encode( $developer_key . ':' . $secret_key );
$headers = array(
                'Authorization' => 'Basic ' . $auth,
                'Content-Type' => 'application/json'
            );
$args = array(
                'headers'     => $headers
            );
$response = wp_remote_post( $url, $args );

Does Not Work:

$url = 'https://api.vimeo.com/oauth/authorize/client';
$auth = base64_encode( $developer_key . ':' . $secret_key );
$data = array(
 'grant_type' => 'client_credentials',
 'scope' => 'public private'
);
$headers = array(
    'Authorization' => 'Basic ' . $auth,
    'Content-Type' => 'application/json'
);
$args = array(
    'headers'     => $headers,
    'data'        => $data
);

Hmmm. As of WordPress 4.6, the WP_HTTP class is now built on Requests by Ryan McCue.

So it seems my question is really also a question about how wp_remote_post() constructs the request. There does not seem to be a way to pass the parameters to the function and have them stringified in the URL.