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

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

概述我在我的Android应用程序中使用oauth2.0与混合流程进行谷歌登录https://developers.google.com/identity/sign-in/web/server-side-flow.我将一次授权代码放入android应用程序并通过postman将其发布到我的flaskapi.当我在api中将flow.step2_exchange应用于此一次auth代码时,它会

我在我的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 STEPfrom oauth2client.clIEnt import flow_from_clIEntsecretsfrom oauth2client.clIEnt import FlowExchangeErrorimport httplib2import Jsonfrom flask import make_responseimport requestsapp = 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 sessionengine = create_engine('sqlite:///restaurantmenu.db')Base.Metadata.bind = engineDBSession = 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 – 401 Unauthorized error.Failed将授权代码升级为凭证对象全部内容,希望文章能够帮你解决android – 401 Unauthorized error.Failed将授权代码升级为凭证对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1118373.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存