ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 401 Unauthorized error.Failed将授权代码升级为凭证对象

2019-07-02 01:13:41  阅读:271  来源: 互联网

标签:android python-3-x oauth-2-0 flask google-oauth2


我在我的Android应用程序中使用oauth 2.0与混合流程进行谷歌登录https://developers.google.com/identity/sign-in/web/server-side-flow.我将一次授权代码放入android应用程序并通过postman将其发布到我的flask api.当我在api中将flow.step2_exchange应用于此一次auth代码时,它会给我流量交换错误.我已经检查了到达api的auth代码与我在应用程序中获得的代码相同.我找不到错误的原因.

我的一次授权代码如下所示:4 / qXilPdy7xOVe5swCBlVRrxjuVu8zEzfcmidlooo7_ls

我的烧瓶api的代码片段:

# IMPORTS FOR THIS STEP
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import FlowExchangeError
import httplib2
import json
from flask import make_response
import requests


app = Flask(__name__)

CLIENT_ID = json.loads(
    open('client_secrets.json', 'r').read())['web']['client_id']
APPLICATION_NAME = "OAUTH_SERVER"
SCOPES = [
    'https://www.googleapis.com/auth/gmail.readonly',
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile',
    # Add other requested scopes.
]

# Connect to Database and create database session
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)
session = DBSession()

@app.route('/gconnect', methods=['POST'])
def gconnect():
    request.get_data()
    code = request.data.decode('utf-8')
    print (code)

    # Upgrade the authorization code into a credentials object
    oauth_flow = flow_from_clientsecrets('client_secrets.json', scope = SCOPES)
    oauth_flow.redirect_uri = 'postmessage'
    try:
        credentials = oauth_flow.step2_exchange(code)
        if credentials is None:
            print ("it is empty")
    except FlowExchangeError:
        response = make_response(
            json.dumps('Failed to upgrade the authorization code.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

我对Api的client_secret.json命名为OAUTH_SERVER,它如下:

{"web":
      {"client_id":"matches the one in console.apps.googleusercontent.com",
"project_id":"oauthapi",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"###########",
"redirect_uris["http://localhost:5000/gconnect","http://localhost:5000/"],
"javascript_origins":["http://localhost:5000"]}
}

解决方法:

我认为这是来自Udacity课程 – 身份验证&授权.请检查login.html是否包含正确的data-clientid值.我有同样的问题,因为复制后忘了改变我的.

标签:android,python-3-x,oauth-2-0,flask,google-oauth2
来源: https://codeday.me/bug/20190702/1352452.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有