我正在尝试使用 Python 和 Django 1.9 为苹果发送身份验证请求,但总是给我unsupported_grant_type
:
def login_with_apple(self, code):
apple_url = "https://appleid.apple.com/auth/token"
client_secret = generate_apple_client_secret()
adapter = AppleOAuth2Adapter(self.request)
headers = {'content-type': 'application/x-www-form-urlencoded'}
data = {'client_id': settings.CLIENT_ID,
'client_secret': client_secret,
'code': code,
'grand_type': 'authorization_code'}
resp = requests.post(url=apple_url, data=data, headers=headers)
access_token = None
if resp.status_code in [200, 201]:
try:
access_token = resp.json()
except ValueError:
access_token = dict(parse_qsl(resp.text))
if not access_token or 'acces_token' not in access_token:
raise OAuth2Error(
'Error retrieving access token: %s' % resp.content
)
return access_token
我如何生成我的client_secret
:
def generate_apple_client_secret():
now = datetime.utcnow()
claims = {
'iss': settings.SOCIAL_AUTH_APPLE_TEAM_ID,
'aud': 'https://appleid.apple.com',
'iat': now,
'exp': now + timedelta(days=180),
'sub': settings.CLIENT_ID,
}
headers = {'kid': settings.SOCIAL_AUTH_APPLE_KEY_ID, 'alg': 'HS256'}
client_secret = jwt.encode(
payload=claims, key=settings.SOCIAL_AUTH_APPLE_PRIVATE_KEY,
algorithm='HS256',
**strong text** headers=headers).decode('utf-8')
return client_secret
我正在向苹果发送请求,但总是给我这个错误:
user = self.login_with_apple(ser.instance.token)
File "/home/omar/PycharmProjects/Aswaq/oscarapi/views/login.py", line 488, in
login_with_apple
Error retrieving access token: %s' % resp.content
OAuth2Error: Error retrieving access token: {"error":"unsupported_grant_type"}