首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我尝试使用python和django 1.9为苹果发送身份验证请求,但总是给我unsupported_grant_type

我尝试使用python和django 1.9为苹果发送身份验证请求,但总是给我unsupported_grant_type
EN

Stack Overflow用户
提问于 2020-10-27 16:06:27
回答 1查看 278关注 0票数 0

我尝试使用Python和Django 1.9为apple发送身份验证请求,但总是给我unsupported_grant_type

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

我正在向apple发送请求,但总是出现以下错误:

代码语言:javascript
运行
复制
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"}
EN

Stack Overflow用户

发布于 2021-09-01 21:36:42

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64550314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档