加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 尝试使用stormpath实现基于oauth / token的登录

发布时间:2020-12-20 13:12:20 所属栏目:Python 来源:网络整理
导读:我的代码: from stormpath.api_auth import ApiRequestAuthenticator import base64 application = app.stormpath_manager.application account = application.accounts[0] # random account new_api_key = account.api_keys.create() authenticator = ApiR
我的代码:

from stormpath.api_auth import ApiRequestAuthenticator
            import base64

            application = app.stormpath_manager.application
            account = application.accounts[0] # random account
            new_api_key = account.api_keys.create()
            authenticator = ApiRequestAuthenticator(application)
            uri = 'dont_care'
            http_method = 'GET'
            headers = {
                'Authorization': 'Basic ' + base64.b64encode(new_api_key.id + ":" + new_api_key.secret)
            }
            result = authenticator.authenticate(headers=headers,http_method=http_method,uri=uri,body={},scopes=[])
            print result.api_key #<ApiKey href=https://api.stormpath.com/v1/apiKeys/bla_bla>
            print result.account # bla_bla@gmail.com
            print result.token # None

我错过了什么?
我的代码基于this part of the documentation.

解决方法

Heyo,我是这个图书馆的作者,所以我想跳到这里=)

你想要做的就是这样.我假设您尝试使用基于用户名/密码的登录而不是API密钥登录,对吗? (EG:你有一个像Angular这样的前端客户端,还是一个提出这些请求的移动应用程序?)

如果是这样,你可以在这里用我的例子完成你想要的东西:

from os import environ

from stormpath.api_auth import PasswordGrantAuthenticator
from stormpath.client import Client


STORMPATH_CLIENT_APIKEY_ID = environ['STORMPATH_CLIENT_APIKEY_ID']
STORMPATH_CLIENT_APIKEY_SECRET = environ['STORMPATH_CLIENT_APIKEY_SECRET']
STORMPATH_APPLICATION_NAME = environ['STORMPATH_APPLICATION_NAME']
STORMPATH_USER_EMAIL = environ['STORMPATH_USER_EMAIL']
STORMPATH_USER_PASSWORD = environ['STORMPATH_USER_PASSWORD']


client = Client(id=STORMPATH_CLIENT_APIKEY_ID,secret=STORMPATH_CLIENT_APIKEY_SECRET)
application = client.applications.query(name=STORMPATH_APPLICATION_NAME)[0]


authenticator = PasswordGrantAuthenticator(app=application)
result = authenticator.authenticate(STORMPATH_USER_EMAIL,STORMPATH_USER_PASSWORD)

if result:
    print('Access Token: {}'.format(result.access_token))
    print('Refresh Token: {}'.format(result.refresh_token))
else:
    print('Invalid credentials supplied.')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读