本文介绍了如何将Postman OAuth 2.0转换为Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将Postman OAuth 2.0转换为Python3时遇到了问题。我尝试研究,但对我来说似乎很不幸,我没有找到任何示例
这是我的代码:

I have been getting the issue when I'm trying to convert Postman OAuth 2.0 to Python3. I tried to research but seems unfortunate for me, I did not find any exampleHere is my code:

from rauth import OAuth2Service
import json

def get_token(client_id, client_secret):
    access_token = None

    service = OAuth2Service(
        name="Viafoura",
        client_id=client_id,
        client_secret=client_secret,
        access_token_url='https://auth.viafoura.io/authorize_client'
    )

    data = {
            'scope': 'xxxxx-xxxx-xxxx-xxxx-xxxxx',
            'grant_type': 'client_credentials'
        }

    session = service.get_auth_session(data=data)

    access_token = session

此处是Postman上的OAuth 2.0,并且可以正常工作:

Here is OAuth 2.0 on Postman and it's working:

我想通过Python3获取access_token。有人可以帮我吗?

I want to get the access_token via Python3. Could anyone please help me on it?

推荐答案

也许可以为您提供帮助,例如OAuth2基本算法的示例

Maybe it can help you, an example with the basic algorithm of OAuth2

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from requests import post, auth, exceptions
from json import loads

if __name__ == '__main__':

    client_id = ''
    client_secret = ''
    user = ''
    password = ''

    access_point = 'https://account.lab.fiware.org/oauth2/token'
    grant_type = 'password'

    headers = {'Content-Type': 'application/x-www-form-urlencoded'}

    auth = auth.HTTPBasicAuth(client_id, client_secret)

    data = {'grant_type': grant_type,
            'username': user,
            'password': password}

    resp = None
    try:
        resp = post(access_point, auth=auth, data=data, headers=headers, timeout=5)
    except exceptions.ConnectionError:
        exit(1)

    if resp.status_code == 200:
        resp = loads(resp.text)
        if 'access_token' in resp:
            print(resp['access_token'])
            exit(0)

    exit(1)

您需要修复访问点,授予类型。可以在找到该源代码>

You need to fix access point, grant type. This source code can be found here

抱歉,我不能直接使用Viafoura和OAuth2Service库。

Sorry, I can't help directly with Viafoura and OAuth2Service library.

这篇关于如何将Postman OAuth 2.0转换为Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:31