Get an Access Token for REST Api

Access Token

This is a key provided by Paypal once it has verified our app and account as being authorized in Paypal. We will mostly use access token for REST Api calls in Paypal. Access token helps uniquely identify the authorized bearer of Paypal.

The Fields in Response we receive

  • access_token : The unique key we are trying to gain
  • token_type : The specification for the key , here it is authorized as being a bearer key.
  • app_id : Our app ID, make sure this is the ID of your app.
  • expires_in : Each token has an expiry date specified in seconds, for eg. 28800 . So make sure to gain your access token right before you intend to use one.

Before we can get an access token we need to create a developers account and our app on from there we will need our app credentials, to get a head start and understand more about each fields we use visit Getting Started.
We will need, (both of these fields are used to authorize our app for payment)

  • Username : This is the ClientID of the app we’ve created.
  • Password : This is the Secret Key which is obtained along side the ClientID of our app.

Sample HttpPost Request to receive access token in android;

HttpRequest Body 

By default use :

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair(“grant_type”, “client_credentials”));

HttpPost Url

HttpPost Header

String base64 = Base64.encodeToString((“<Username>”+”:”+”<Password>”).getBytes(“UTF-8”), Base64.URL_SAFE|Base64.NO_WRAP);

post.setHeader(“Accept”, “application/json”);

post.setHeader(“Accept-Language”, “en_US”);

post.setHeader(“Content-Type”, “application/x-www-form-urlencoded;charset=UTF-8”);

post.setHeader(“Authorization”,”Basic “+base64);

Note : The authorization field must be Base64 string encoded.


One thought on “Get an Access Token for REST Api

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s