Obtain profile information
You can obtain the user's profile information from Amazon Pay by using the access token returned by the Button widget. An access token is granted by the authorization server when a user signs in to a site. An access token is specific to a client, a user, and an access scope. A client must use an access token to retrieve customer profile data. In your server-side application, handle the request made to /handle_login.php, and obtain profile information by using the access token and the Profile REST API.
Examples
PHP example
$c = curl_init('https://api.sandbox.amazon.de/auth/o2/tokeninfo?access_token='
. urlencode($_REQUEST['access_token']));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$r = curl_exec($c);
curl_close($c);
$d = json_decode($r);
if ($d->aud != 'YOUR-CLIENT-ID') {
// the access token does not belong to us
header('HTTP/1.1 404 Not Found');
echo 'Page not found';
exit;
}
// exchange the access token for user profile
$c = curl_init('https://api.sandbox.amazon.de/user/profile');
curl_setopt($c, CURLOPT_HTTPHEADER, array('Authorization: bearer '
. $_REQUEST['access_token']));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$r = curl_exec($c);
curl_close($c);
$d = json_decode($r);
echo sprintf('%s %s %s', $d->name, $d->email, $d->user_id);
Java example
You need to download the Jackson and HttpComponents libraries to use this sample code.
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import java.net.URLEncoder;
import java.util.Map;
...
// verify that the access token belongs to us
Content c = Request.Get(
"https://api.sandbox.amazon.de/auth/o2/tokeninfo?access_token=" +
URLEncoder.encode(access_token, "UTF-8"))
.execute()
.returnContent();
Map m = new ObjectMapper().readValue(
c.toString(), new TypeReference>(){});
if (!"YOUR-CLIENT-ID".equals(m.get("aud"))) {
// the access token does not belong to us
throw new RuntimeException("Invalid token");
}
// exchange the access token for user profile
c = Request.Get("https://api.sandbox.amazon.de/user/profile")
.addHeader("Authorization", "bearer " + access_token)
.execute()
.returnContent();
m = new ObjectMapper().readValue(
c.toString(), new TypeReference>(){});
System.out.println(
String.format("%s %s %s", m.get("name"),
m.get("email"), m.get("user_id")));
For more information, see the Login with Amazon Developer's Guide for Websites.