在 Python 中从 Paypal 获取访问令牌 - 使用 urllib2 或 requests 库 [英] Get access token from Paypal in Python - Using urllib2 or requests library

查看:16
本文介绍了在 Python 中从 Paypal 获取访问令牌 - 使用 urllib2 或 requests 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

卷曲

curl -v https://api.sandbox.paypal.com/v1/oauth2/token -H "接受:应用程序/json";-H "Accept-Language: en_US";-u "client_id:client_secret";-d "grant_type=client_credentials";

参数:-uclient_id:client_secret

这里我传递了我的 client_idclient_secret,它在 cURL 中正常工作.

我正在尝试在 Python 上实现相同的东西

Python

导入 urllib2导入 base64token_url = 'https://api.sandbox.paypal.com/v1/oauth2/token'client_id = '.....'client_secret = '....'凭证 = %s:%s"% (client_id, client_secret)encode_credential = base64.b64encode(credentials.encode('utf-8')).decode('utf-8').replace("
", "")header_params = {授权":(基本 %s"% encode_credential),内容类型":应用程序/x-www-form-urlencoded",接受":应用程序/json";}参数 = {'grant_type': 'client_credentials',}request = urllib2.Request(token_url, param, header_params)响应 = urllib2.urlopen(请求)打印Response______",响应

追溯:

<块引用>

result = urllib2.urlopen(request)

 HTTPError: HTTP Error 400: Bad Request

你能告诉我我的python代码有什么问题吗?

解决方案

Late answer,但截至 2021 年,我使用以下 python 代码生成新的不记名令牌:

如果您还没有这样做,请

Python 代码:

导入请求d = {grant_type":client_credentials"}h = {Accept":application/json",Accept-Language":en_US"}cid = ASOGsGWr7yxepDuthbkKL-WoGNVAS7O0XlZ2ejcWsBA8ZXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"秘密 = 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']

来源:

  1. https://developer.paypal.com/developer/applications/莉>
  2. https://www.paypal.com/smarthelp/article/how-do-i-get-an-access-token-ts2128

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("
", "")

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?

解决方案

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.

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

这篇关于在 Python 中从 Paypal 获取访问令牌 - 使用 urllib2 或 requests 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆