我正在开发一个需要对BitBucket进行用户身份验证的Django项目,我设置了allauth,以便用户可以使用Bitbucket进行身份验证,我现在还不知道如何向Bitbucket API提出进一步的请求用户已通过身份验证。
我理解allauth纯粹是出于身份验证的目的,没有关于如何访问和进一步使用身份验证的文档,在这种情况下访问凭据(oauth_token),以便我可以代表资源所有者。
答案 0 :(得分:1)
我找到了身份验证详细信息以进一步请求。
from allauth.socialaccount.models import SocialAccount, SocialApp
bitbucket_app = SocialApp.objects.get(provider='bitbucket')
user_account = SocialAccount.objects.get(user=request.user)
# User should only have one SocialToken object per SocialApp
# https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py#L137
user_token = useraccount.socialtoken_set.first()
# Credentials to make further requests
client_key = bitbucket_app.client_id
client_secret = bitbucket_app.secret
resource_owner_key = user_token.token
resource_owner_secret = user_token.token_secret
import requests
from requests_oathlib import OAuth1
auth = OAuth1(client_key, client_secret, resource_owner_key, resource_owner_secret)
r = requests.get(protected_url, auth=auth)
https://bitbucket-api.readthedocs.org/en/latest/index.html
from bitbucket.bitbucket import Bitbucket
bb = Bitbucket(user_account.uid) # Initialise with bitbucket username
bb.authorize(client_key, client_secret, 'http://localhost', resource_owner_key, resource_owner_secret)
# Get user repositories as an example
bb.repository.all()