python 谷歌登录_Google登录 – 服务器端流程 – Python – G...

在将Google登录服务器端流程应用于Flask应用时,开发者遇到问题,用户通过Google登录后,服务器端无法接收到预期的'gplus_id' GET参数。问题可能源于javascript的connectServer函数或App Engine的实现。目前,'gplus_id'为None,而'result['user_id']'是一个有效的用户ID。解决方案可能涉及在javascript中添加'gplus_id'参数,或者检查App Engine端的代码逻辑。
摘要由CSDN通过智能技术生成

我正在使用Flask在Google App Engine上构建应用.我正在从https://developers.google.com/+/web/signin/server-side-flow中描述的服务器端流程实现Google登录.在切换到App Engine之前,我的流程非常相似.也许从那以后我引入了一个错误.或者这可能是我在App Engine中实现的问题.

我相信通过Google登录流重定向的网址应该有一个GET参数设置为“gplus_id”,但是,我没有收到此参数.

我有一个登录按钮创建:

(function() {

var po = document.createElement('script');

po.type = 'text/javascript'; po.async = true;

po.src = 'https://plus.google.com/js/client:plusone.js?οnlοad=render';

var s = document.getElementsByTagName('script')[0];

s.parentNode.insertBefore(po, s);

})();

function render() {

gapi.signin.render('gplusBtn', {

'callback': 'onSignInCallback',

'clientid': '{{ CLIENT_ID }}',

'cookiepolicy': 'single_host_origin',

'requestvisibleactions': 'http://schemas.google.com/AddActivity',

'scope': 'https://www.googleapis.com/auth/plus.login',

'accesstype': 'offline',

'width': 'iconOnly'

});

}

在页面的javascript代码中,我有一个启动流程的功能:

var helper = (function() {

var authResult = undefined;

return {

onSignInCallback: function(authResult) {

if (authResult['access_token']) {

// The user is signed in

this.authResult = authResult;

helper.connectServer();

} else if (authResult['error']) {

// There was an error, which means the user is not signed in.

// As an example, you can troubleshoot by writing to the console:

console.log('GPlus: There was an error: ' + authResult['error']);

}

console.log('authResult', authResult);

},

connectServer: function() {

$.ajax({

type: 'POST',

url: window.location.protocol + '//' + window.location.host + '/connect?state={{ STATE }}',

contentType: 'application/octet-stream; charset=utf-8',

success: function(result) {

// After we load the Google+ API, send login data.

gapi.client.load('plus','v1',helper.otherLogin);

},

processData: false,

data: this.authResult.code,

error: function(e) {

console.log("connectServer: error: ", e);

}

});

}

}

})();

/**

* Calls the helper method that handles the authentication flow.

*

* @param {Object} authResult An Object which contains the access token and

* other authentication information.

*/

function onSignInCallback(authResult) {

helper.onSignInCallback(authResult);

}

这将启动“/ connect”处的流程(请参阅the above doc中引用的步骤8.):

@app.route('/connect', methods=['GET', 'POST'])

def connect():

# Ensure that this is no request forgery going on, and that the user

# sending us this connect request is the user that was supposed to.

if request.args.get('state', '') != session.get('state', ''):

response = make_response(json.dumps('Invalid state parameter.'), 401)

response.headers['Content-Type'] = 'application/json'

return response

# Normally the state would be a one-time use token, however in our

# simple case, we want a user to be able to connect and disconnect

# without reloading the page. Thus, for demonstration, we don't

# implement this best practice.

session.pop('state')

gplus_id = request.args.get('gplus_id')

code = request.data

try:

# Upgrade the authorization code into a credentials object

oauth_flow = client.flow_from_clientsecrets('client_secrets.json', scope='')

oauth_flow.redirect_uri = 'postmessage'

credentials = oauth_flow.step2_exchange(code)

except client.FlowExchangeError:

app.logger.debug("connect: Failed to upgrade the authorization code")

response = make_response(

json.dumps('Failed to upgrade the authorization code.'), 401)

response.headers['Content-Type'] = 'application/json'

return response

# Check that the access token is valid.

access_token = credentials.access_token

url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s'

% access_token)

h = httplib2.Http()

result = json.loads(h.request(url, 'GET')[1])

# If there was an error in the access token info, abort.

if result.get('error') is not None:

response = make_response(json.dumps(result.get('error')), 500)

response.headers['Content-Type'] = 'application/json'

return response

# Verify that the access token is used for the intended user.

if result['user_id'] != gplus_id:

response = make_response(

json.dumps("Token's user ID doesn't match given user ID."), 401)

response.headers['Content-Type'] = 'application/json'

return response

...

但是,如果result [‘user_id’]!= gplus_id:,则表示“Token的用户ID与给定的用户ID不匹配”,流程将停止. result [‘user_id’]是有效的用户ID,但gplus_id为None.

行gplus_id = request.args.get(‘gplus_id’)期望GET参数包含’gplus_id’,但它们只包含’state’.这是我的javascript connectServer函数的问题吗?我应该在那里加上’gplus_id’吗?当然我当时不知道.或者是其他东西?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值