我正在尝试为网络应用程序创建登录功能,并且一直按照此处提供的指南https://developers.google.com/identity/sign-in/web/server-side-flow
我按照说明主要复制所有内容并将其替换为我的信息。我希望尽可能保持基本,这样我才能理解正在发生的事情。
这是我的身份验证Python应用程序。我认为我已经完成了http请求错误,因为我必须找到其他示例,因为它没有在谷歌网站上解释。 该网站应该将auth_code发送到http://localhost:5000/auth_code,web.py应该监听它,如果成功则打印出电子邮件。
web.py
from apiclient import discovery
import httplib2
from oauth2client import client
import json
h = httplib2.Http(".cache")
resp, content = h.request("http://localhost:5000/auth_code", "GET")
auth_code = content
print (auth_code)
CLIENT_SECRET_FILE = '*****.apps.googleusercontent.com.json'
credentials = client.credentials_from_clientsecrets_and_code(
CLIENT_SECRET_FILE, ['https://www.googleapis.com/auth/drive.appdata', 'profile', 'email'],
auth_code)
http_auth = credentials.authorize(httplib2.Http())
drive_service = discovery.build('drive', 'v3', http=http_auth)
appfolder = drive_service.files().get(fileID='appfolder').execute()
userid = credentials.id_token['sub']
email = credentials.id_token['email']
print (email)
这是用于提供网页的python应用程序。
host.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/') def index():
return render_template('index.html')
if __name__=="__main__": app.run(debug=True)
这是网页的HTML。
<!-- The top of file index.html --> <html itemscope itemtype="http://schema.org/Article"> <head> <!-- BEGIN Pre-requisites --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script> <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer> </script> <!-- END Pre-requisites --> <!-- Continuing the <head> section --> <script>
function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: '****.apps.googleusercontent.com',
// Scopes to request in addition to 'profile' and 'email'
//scope: 'additional_scope'
});
});
} </script> </head> <body> <!-- ... --> <!-- Add where you want your sign-in button to render --> <!-- Use an image that follows the branding guidelines in a real app --> <button id="signinButton">Sign in with Google</button> <script> $('#signinButton').click(function() {
// signInCallback defined in step 6.
auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(signInCallback); }); </script> <script> function signInCallback(authResult) { if (authResult['code']) {
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
// Send the code to the server
$.ajax({
type: 'POST',
url: 'http://localhost:5000/auth_code',
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
// Handle or verify the server response.
},
processData: false,
data: authResult['code']
}); } else {
// There was an error. } } </script> </body> </html>