“unsupported_grant_type”当使用 Google 的 urlfetch 从 PayPal 获取 oauth 令牌时。使用“请求”作品

发布于 2025-01-20 07:00:12 字数 1302 浏览 2 评论 0原文

我一直在尝试(惨败)使用Google的Urlfetch模块(App Engine在本地服务器中的Python)从PayPal检索令牌。它使用以下操作,使用App Engine的“请求”模块外部

    url = base + "/v1/oauth2/token"

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


    auth_encoded = APP_CLIENT_ID + ":" + APP_SECRET
    auth_encoded = base64.b64encode(auth_encoded)

    ##headers = {'Content-Type': 'application/json', 'Authorization': 'Basic ' + auth_encoded}
    headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + auth_encoded}

    r = requests.post(url,headers=headers,params=payload)
    print r.text

...但是,我在使用urlfetch尝试同一件事时会收到此消息:

{“ error”:“ unsupported_grant_type”,“ error_description”:“ grant类型为null}

...这是我使用的代码:

    url = base + "/v1/oauth2/token"

    payload = {"grant_type": "client_credentials"}

    auth_encoded = APP_CLIENT_ID + ":" + APP_SECRET
    auth_encoded = base64.b64encode(auth_encoded)

    headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + auth_encoded}

    result = urlfetch.fetch(
        url=url,
        method=urlfetch.POST,
        headers=headers,
        payload = payload
    )

...我尝试了所有我能想到的所有内容。应该是一件简单的事情。

I've been trying (and failing miserably) to use google's urlfetch module (python within app engine's local server) to retrieve a token from paypal. It works as follows using the "requests" module outside of app engine:

    url = base + "/v1/oauth2/token"

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


    auth_encoded = APP_CLIENT_ID + ":" + APP_SECRET
    auth_encoded = base64.b64encode(auth_encoded)

    ##headers = {'Content-Type': 'application/json', 'Authorization': 'Basic ' + auth_encoded}
    headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + auth_encoded}

    r = requests.post(url,headers=headers,params=payload)
    print r.text

... but I get this message when trying the same thing with urlfetch:

{"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}

... here's the code that I'm using:

    url = base + "/v1/oauth2/token"

    payload = {"grant_type": "client_credentials"}

    auth_encoded = APP_CLIENT_ID + ":" + APP_SECRET
    auth_encoded = base64.b64encode(auth_encoded)

    headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + auth_encoded}

    result = urlfetch.fetch(
        url=url,
        method=urlfetch.POST,
        headers=headers,
        payload = payload
    )

... I've tried everything that I can think of. Should be a simple thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悟红尘 2025-01-27 07:00:12

此 API 调用的格式为 application/x-www-form-urlencoded ,而不是 JSON。

因此:

payload = "grant_type=client_credentials"

import urllib
payload = urllib.urlencode({"grant_type": "client_credentials"})

This API call is formatted as application/x-www-form-urlencoded , not JSON.

Therefore:

payload = "grant_type=client_credentials"

or

import urllib
payload = urllib.urlencode({"grant_type": "client_credentials"})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文