【问题标题】:How to convert Postman OAuth 2.0 to Python如何将 Postman OAuth 2.0 转换为 Python
【发布时间】:2020-01-03 06:31:33
【问题描述】:

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

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,它正在运行:

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

【问题讨论】:

    标签: python-3.x oauth-2.0 postman


    【解决方案1】:

    或许对你有帮助,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)
    

    您需要修复访问点、授权类型。这个源码可以找到here

    抱歉,我无法直接帮助 Viafoura 和 OAuth2Service 库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 2016-11-09
      • 2021-01-30
      • 1970-01-01
      相关资源
      最近更新 更多