0

Web アプリケーションのログイン機能を作成しようとしており、ここに記載されているガイドに従っていますhttps://developers.google.com/identity/sign-in/web/server-side-flow

私はほとんどすべてをコピーして自分の情報に置き換える指示に従いました。何が起こっているのかを理解できるように、できるだけ基本的なものにしておきたいと思います。

認証用の Python アプリケーションを次に示します。Google Web サイトで説明されていないため、他の例を見つける必要があったため、http 要求を間違って実行したと思います。Web サイトは 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)

これは、Web ページを提供するための Python アプリです。

ホスト.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)

これは Web ページの 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>
4

0 に答える 0