Amazon login

This tutorial will show you how to amazon login, first things first, you need amazon account and create new application that allows you using api. To create new one, visit this create amazon application.

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

Note: Amazon oauth flow require redirect_uri must start with https, amazon 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

amazon project 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_amazon_config() {
    return $config = array(
    'client_id' => 'your-client-id',
    'client_secret' => 'client-secret',
    'redirect_uri' => self::get_base_url('amazon-callback.php'),
    'login' => self::get_base_url('amazon-login.php'),
    'session' => 'amazon_access_token'
    );
    }

Create amazon-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

  •  $authen_url = 'https://www.amazon.com/ap/oa?' .
    'client_id=' . $config['client_id'] .
    '&scope=profile' .
    '&response_type=code' .
    '&redirect_uri=' . $config['redirect_uri'] .
    '&state=1123';
    echo '<a href="' . $authen_url . '">Log in with Amazon</a>';

- Send request to get user's profile and print result on screen

  •  $access_token = $_SESSION[$config['session']];
    $endpoint = 'https://api.amazon.com/user/profile';
    $header = array(
    'GET /user/profile HTTP/1.1',
    'Host: api.amazon.com',
    'Authorization: Bearer ' . $access_token['access_token']
    );
    $data = projecthelper::make_request($endpoint, $header);
    var_dump($data);

<?php

session_start();
include 'projecthelper.php';

$config = projecthelper::get_amazon_config();

if (isset($_SESSION[$config['session']])) {
$access_token = $_SESSION[$config['session']];
$endpoint = 'https://api.amazon.com/user/profile';
$header = array(
'GET /user/profile HTTP/1.1',
'Host: api.amazon.com',
'Authorization: Bearer ' . $access_token['access_token']
);
$data = projecthelper::make_request($endpoint, $header);
var_dump($data);
} else {
$authen_url = 'https://www.amazon.com/ap/oa?' .
'client_id=' . $config['client_id'] .
'&scope=profile' .
'&response_type=code' .
'&redirect_uri=' . $config['redirect_uri'] .
'&state=1123';
echo '<a href="' . $authen_url . '">Log in with Amazon</a>';
}

Create amazon-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'];
    $endpoint = 'https://api.amazon.com/auth/o2/token';
    $header = array(
    'POST /auth/o2/token HTTP/1.1',
    'Host: api.amazon.com',
    'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
    );
    $post_fields = 'client_id=' . $config['client_id'] .
    '&client_secret=' . $config['client_secret'] .
    '&code=' . $code .
    '&grand_type==authorization_code';
    $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

session_start();
include 'projecthelper.php';

$config = projecthelper::get_amazon_config();
if (isset($_GET['code'])) {
$code = $_GET['code'];
$endpoint = 'https://api.amazon.com/auth/o2/token';
$header = array(
'POST /auth/o2/token HTTP/1.1',
'Host: api.amazon.com',
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
);
$post_fields = 'client_id=' . $config['client_id'] .
'&client_secret=' . $config['client_secret'] .
'&code=' . $code .
'&grand_type==authorization_code';
$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