JavaScript ClientããApp Engineã®Cloud Endpointsã§Oauth2èªè¨¼ããæ¹æ³
試è¡é¯èª¤ãã¦ã§ããããã«ãªã£ãã®ã§ãç°¡åã«ã¾ã¨ããã
åºæ¬çã«ã¯ããã«æ¸ãã¦ããã¨ããã
To use a Google Cloud Endpoint, you'll need to use the Google JavaScript client library. The code you need to add to use Endpoints is minimal, consisting of:
Google App Engine — Google Developers
var ROOT = 'https://<your-app-id>.appspot.com/_ah/api'; var CLIENT_ID = 'xxxxxxxxxxxxxxx.apps.googleusercontent.com'; var SCOPES = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'; function init() { var apisToLoad; var callback = function() { if (--apisToLoad == 0) { signin(true, userAuthed); } } apisToLoad = 2; // must match number of calls to gapi.client.load() gapi.client.load('<your-app-id>', 'v1', callback, ROOT); gapi.client.load('oauth2', 'v2', callback); } function signin(mode, callback) { gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: mode}, callback); } function userAuthed() { var request = gapi.client.oauth2.userinfo.get().execute(function(resp) { if (!resp.code) { // èªè¨¼å¾ã®å¦ç } }); }
æåã®ã¯ã¾ããã¤ã³ãã¯ã401ã¨ã©ã¼ãä¸è¨ã®ã³ã¼ãã ãã ã¨èªè¨¼ã®ã¦ã£ã³ãã¦ãéããªããã¨ããã®ã¯ãinit()ã®ä¸ã§signin(true, hoge)ãå¼ã³åºãã¦ããããããã¨ããã®å
ã§å¼ã³åºãã¦ã gapi.auth.authorize() ã® mode ã« true ã渡ãããã
æåã«æ¸ããããGoogle App Engine — Google Developersãã®ä¸ç¨ã«ãã©ã¡ã¼ã¿ã®èª¬æãããã
mode is either false or true, depending on whether or not youâd like to present the explicit authorization window to the user. If the user has never authorized your application before, calling authorize with mode: true (no authorization window) will fail. The correct situation to use each modes is described below.
ã¦ã¼ã¶ã¼ãä¸åº¦ãèªè¨¼ãã¦ãªãã£ãããtrueã ã¨ï¼èªè¨¼ã¦ã£ã³ãã¦ãéããªãããï¼å¤±æãããã£ã¦æ¸ãã¦ããã
ãªã®ã§ã次ã®ã³ã¼ãã追å ãã¦ããã£ã¦æ¸ãã¦ããã
<a href="#" onclick="auth();" id="signinButton">Sign in!</a> function auth() { signin(false, userAuthed); };
ã§ã追å ãããªã³ã¯ãã¯ãªãã¯ããã¨ã¦ã£ã³ãã¦ãéãããã«ã¯ãªããã©ã表示å 容ã¯èªè¨¼ã®å 容ã§ã¯ãªãã¦ã¨ã©ã¼ã次ã®ãã¼ãã«ããã¨ã©ã¼ origin_mismatchãã ã£ãã
ããã£ãããStackOverflow ã«ãhttp 㨠https ã®éãã§ããã£ã¦æ¸ãã¦ãã£ãããããã¿ã³ãè¨ç½®ãã¦ãããã¼ã¸ã®URLã¹ãã¼ã ã http ãã https ã«å¤ãã¦ããã ãã§ãã£ããã¨èªè¨¼ã§ããããã«ãªã£ãã
ã¤ã¾ãããã¼ã«ã«ã§ OAuth2 èªè¨¼ã®ãã¹ããããã¨ããã¨ããã¼ã«ã«ã®Jettyã« https ã§ã¢ã¯ã»ã¹ã§ããããã«ããå¿ è¦ãããã®ã§è¦æ³¨æã