5

cURL

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
  -H "Accept: application/json" \
  -H "Accept-Language: en_US" \
  -u "client_id:client_secret" \
  -d "grant_type=client_credentials"

Parameters: -u take client_id:client_secret

Here I pass my client_id and client_secret, It's worked properly in cURL.

I am trying to same things implement on Python

Python

import urllib2
import base64
token_url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
client_id = '.....'
client_secret = '....'

credentials = "%s:%s" % (client_id, client_secret)
encode_credential = base64.b64encode(credentials.encode('utf-8')).decode('utf-8').replace("\n", "")

header_params = {
    "Authorization": ("Basic %s" % encode_credential),
    "Content-Type": "application/x-www-form-urlencoded",
    "Accept": "application/json"
}
param = {
    'grant_type': 'client_credentials',
}

request = urllib2.Request(token_url, param, header_params)
response = urllib2.urlopen(request)
print "Response______", response

Traceback:

result = urllib2.urlopen(request)

 HTTPError: HTTP Error 400: Bad Request

Can you inform me whats wrong with my python code?

Community
  • 1
  • 1
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76

4 Answers4

12

Late answer, but as of 2021, I use the following python code to generate a new bearer token:

If you haven't done so, create a new live app on developer.paypal.com
You'll receive a Client ID and a Secret, which you'll use to generate the Bearer Token.

enter image description here

Python Code:

import requests

d = {"grant_type" : "client_credentials"}
h = {"Accept": "application/json", "Accept-Language": "en_US"}

cid = "ASOGsGWr7yxepDuthbkKL-WoGNVAS7O0XlZ2ejcWsBA8ZXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
secret = "EJTKAFEYfN9IaVHc4Y-MECzgBivt2MfW6rcyfbVky0T07yRwuuTdXOczuCoEIXXXXXXXXXXXXXXX"

r = requests.post('https://api.paypal.com/v1/oauth2/token', auth=(cid, secret), headers=h, data=d).json()
access_token = r['access_token']

Sources:

  1. https://developer.paypal.com/developer/applications/
  2. https://www.paypal.com/smarthelp/article/how-do-i-get-an-access-token-ts2128
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
3

I would suggest using requests:

import requests
import base64

client_id = ""
client_secret = ""

credentials = "%s:%s" % (client_id, client_secret)
encode_credential = base64.b64encode(credentials.encode('utf-8')).decode('utf-8').replace("\n", "")

headers = {
    "Authorization": ("Basic %s" % encode_credential),
    'Accept': 'application/json',
    'Accept-Language': 'en_US',
}

param = {
    'grant_type': 'client_credentials',
}

url = 'https://api.sandbox.paypal.com/v1/oauth2/token'

r = requests.post(url, headers=headers, data=param)

print(r.text)
heinst
  • 8,520
  • 7
  • 41
  • 77
0

It needs URL encoding:

param = {
  'grant_type': 'client_credentials',
}

data = urllib.urlencode(param)
request = urllib2.Request(token_url, data, header_params)
Hans Z.
  • 50,496
  • 12
  • 102
  • 115
0

This was the answer that worked for me with python 3.7. I needed to apply a base64 encoding to my auth credentials:

import base64
import requests
from backend.config import Config


def get_paypal_access_token(client_id=Config.PAYPAL_CLIENT_ID, secret=Config.PAYPAL_SECRET):
    url = "https://api.sandbox.paypal.com/v1/oauth2/token"
    payload = 'grant_type=client_credentials'
    encoded_auth = base64.b64encode((Config.PAYPAL_CLIENT_ID + ':' + Config.PAYPAL_SECRET).encode())
    headers = {
      'Authorization': f'Basic {encoded_auth.decode()}',
      'Content-Type': 'application/x-www-form-urlencoded'
    }
    r = requests.request("POST", url, headers=headers, data=payload)
    assert r.status_code == 200
    return r.json()["access_token"]

I handle my client ID and secret from PayPal via a Config file, but hopefully it's straightforward how you would swap these out with your own credentials once you've registered your app.

aaron
  • 6,339
  • 12
  • 54
  • 80