Vimeo login

This tutorial will guild you how to vimeo login, at first, you must have vimeo account, create a application to receive client id and secret, for more details please see this tutorial. create vimeo app.

After that, write down app_id and app_secret to get start with vimeo login.

Note: Vimeo login using OAuth 2.0, for more information please read this article.

System requirements

  • PHP 5.4 or higher
  • APACHE 2.4.9 or higher
  • Netbean IDE

Project Structure

 vimeo structure

Implement code

Create projecthelper.php

We wrote one tutorial about projecthelper.php, view more at here, if you were already read it, please add this function in file.

  • public static function get_vimeo_config() {
    return $config = array(
    'client_id' => 'your-client-id',
    'client_secret' => 'client-secret',
    'redirect_uri' => self::get_base_url('vimeo-callback.php'),
    'login' => self::get_base_url('vimeo-login.php'),
    'session' => 'vimeo_access_token'
    );
    }

Create vimeo-login.php

This file will undertake two main tasks, check access_token in $_SESSION, if existed, send request to get user's profile, the opposite will create $authen_url for the user grant access your applications to connect to their accounts.

- Create $authen_url link

  •  $endpoint = 'https://api.vimeo.com/oauth/authorize/';
    $redirect_uri = $config['redirect_uri'];
    $scope = 'public'; // permission
    $state = '12332'; // randon text
    $url = $endpoint .
    '?client_id=' . $config['client_id'] .
    '&redirect_uri=' . $redirect_uri .
    '&response_type=code' .
    '&scope=' . $scope .
    '&state=' . $state;
    echo '<a href="' . $url . '" >Login with vimeo</a>';

- access_token contains user's profile, just print on screen

  •  $access_token = $_SESSION[$config['session']]; 
    var_dump($access_token['user']);

<?php

if (!session_id()) {
session_start();
}
include 'projecthelper.php';

$config = projecthelper::get_vimeo_config();

if (isset($_SESSION[$config['session']])) {
$access_token = $_SESSION[$config['session']];
var_dump($access_token['user']);
} else {
$endpoint = 'https://api.vimeo.com/oauth/authorize/';
$redirect_uri = $config['redirect_uri'];
$scope = 'public'; // permission
$state = '12332'; // randon text
$url = $endpoint .
'?client_id=' . $config['client_id'] .
'&redirect_uri=' . $redirect_uri .
'&response_type=code' .
'&scope=' . $scope .
'&state=' . $state;
echo '<a href="' . $url . '" >Login with vimeo</a>';
}

Create vimeo-callback.php

After granted access to your application, user will be redirected to your redirect_uri with authentication code. Then you will make request to get access_token.
(In some cases, your redirect_uri that you provide must match with the one you've described in app's settings, or redirect_uri must begin with https).

+ Check and extract authentication code

  • Send request to get access_token
  •  $code = $_GET['code'];
    $post_fields = 'grant_type=authorization_code' .
    '&redirect_uri=' . $config['redirect_uri'] .
    '&code=' . $code;

    $header = array(
    'Content-type: application/x-www-form-urlencoded',
    'Content-Length: ' . strlen($post_fields),
    'Authorization: basic ' . base64_encode($config['client_id'] . ':' . $config['client_secret'])
    );

    $data = projecthelper::make_request($endpoint, $header, $post_fields);
  • Get result, if there is no error, save the result get into $_SESSION and navigate user to the login page.
  •  $_SESSION[$config['session']] = $data;
    header('Location: ' . $config['login']);
  • If there are errors, print to the screen
  •  print_r($data);

+ If there are errors, print to the screen

  • print_r($_GET);

<?php

if (!session_id()) {
session_start();
}
include 'projecthelper.php';
$config = projecthelper::get_vimeo_config();
$endpoint = 'https://api.vimeo.com/oauth/access_token/';
$redirect_uri = $config['redirect_uri'];

if (isset($_GET['code'])) {

$code = $_GET['code'];
$post_fields = 'grant_type=authorization_code' .
'&redirect_uri=' . $config['redirect_uri'] .
'&code=' . $code;

$header = array(
'Content-type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($post_fields),
'Authorization: basic ' . base64_encode($config['client_id'] . ':' . $config['client_secret'])
);

$data = projecthelper::make_request($endpoint, $header, $post_fields);

if (is_array($data) && isset($data['access_token'])) {
$_SESSION[$config['session']] = $data;
header('Location: ' . $config['login']);
} else {
print_r($data);
}
} else {
print_r($_GET);
}

After implement code, run project and view result or you can view our example here. If you have questions, don't hesitate, tell us