How to get an Access Token to integrate with PayPal REST APIs in Python

Before you can integrate with PayPal via the REST APIs, you will need to obtain an access token.

Given that REST APIs is agnostic to programming languages, you can use a programming language of your choice to get the Access Token.

In case you are wondering how you can get an Access Token to integrate with PayPal REST APIs in Python, this is how you can do so.

1. Create a PayPal App for your application to integrate with

If you have not done any integration with PayPal before, you will need to create a PayPal App.

In order to do so, login to PayPal developer account. After you had logged in successfully, click on Create App under My apps & credentials:
PayPal developer portal My apps and credentials screen with Create App button highlighted

If you are creating a PayPal App for production integration, then be sure to toggle the Live option at the top.

When the Create New App page loads, fill in your App details and associate it to a PayPal business account.

After you clicked on Create App, you will see the Client ID and Secret:PayPal App my-app details page after the App is created.

You can get the secret by clicking on Show.

Given that, record the Client ID and Secret for later use.

2. Install the Python requests library

If you had not done so, you can install the Python requests library into your Python environment:

pip install requests

After you had installed the requests library, you will have an easy-to-use library to send our HTTP requests.

3. Run the Python codes to get the Access Token from your PayPal App

Given that we have the necessary ingredients, we are ready to write the Python codes to get the Access Token.

To get the Access Token, we are sending a HTTP Basic Authentication request with Python to PayPal.

Given that in mind, the following is a simple Python script to get the the Access Token from your PayPal App:

import requests

# Change the URL to https://api-m.paypal.com for live access
PAYPAL_URL = 'https://api-m.sandbox.paypal.com'
APP_CLIENT_ID = 'your_app_client_id'
APP_SECRET = 'your_app_secret'

oauth_url = '%s/v1/oauth2/token' % PAYPAL_URL

oauth_response = requests.post(oauth_url,
                               headers= {'Accept': 'application/json',
                                         'Accept-Language': 'en_US'},
                               auth=(APP_CLIENT_ID, APP_SECRET),
                               data={'grant_type': 'client_credentials'})

# Get OAuth JSON in response body
oauth_body_json = oauth_response.json()
# Get access token
access_token = oauth_body_json['access_token']
print(access_token)

Before running the script, change your_app_client_id and your_app_secret with the ones that you obtained earlier. After you have done so, you will be able to see the Access Token on your shell console when you run the script.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.