Prywatny stół FusionTables z OAUTH2

Dobre zdjęcie wgTim Rosenberg to dokładnie pokazuje, jak działa OAUTH2:

Jestem uprzejmy, leniwy, aby nawet zacząć na to patrzeć2 pliki itest więc szukałem najłatwiejszej drogi

1. Pobierz token

2. dostęp za pomocą tego tokena

z pomocągwt-oauth2

umieść go w głowie index.php:<script type="text/javascript" src="gwt-oauth2.js"></script>

i to w ciele

<script type="text/javascript">
(function() {
var GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/auth";
var GOOGLE_CLIENT_ID = "CLIENT_ID";
//var PLUS_ME_SCOPE = "https://www.googleapis.com/auth/plus.me";
//var FusionTable_SCOPE = "https://www.googleapis.com/auth/fusiontables";       
var button = document.createElement("button");
button.innerText = "Authenticate with Google";
button.onclick = function() {

var req = {
    'authUrl' : GOOGLE_AUTH_URL,
    'clientId' : GOOGLE_CLIENT_ID,
    'scopes': ['https://www.googleapis.com/auth/plus.me',
               'https://www.googleapis.com/auth/fusiontables'
              ],
};

oauth2.login(req, function(token) {
    alert('Got an OAuth token:\n'+ token +'\n'+ 'Token expires in '+ oauth2.expiresIn(req) +' ms\n');
  }, function(error) {
    alert("Error:\n" + error);
  });
};

var dv = document.getElementById('admin-content');
dv.appendChild(button);
var clearTokens = document.createElement('button');
clearTokens.innerText = 'Clear all tokens'
clearTokens.onclick = oauth2.clearAllTokens;
dv.appendChild(clearTokens);
})();
</script>

DOBRZE,

Teraz możesz zobaczyć połączenie i przekierowanie do oauthWindow.html w nowym oknie bez błędów. GET parametry pokazują terazaccess_token token_type expires_in. Sprawdź token dostępuTUTAJ

Jak widzisz access_token działa świetnie, ALE

To, czego nadal nie dostaniesz, to pierwsze powiadomienie z tego:

oauth2.login(req, function(token) {
  alert('Got an OAuth token:\n' + token + '\n'
  + 'Token expires in ' + oauth2.expiresIn(req) + ' ms\n');
}, function(error) {
  alert("Error:\n" + error);
});

Drugi alert działa dobrze i podczas próby uwierzytelnienia. ponownie, jeśli oauthWindow.html nadal jest otwarty, wyświetli alert o błędzie (więc działa!) Dodajmy teraz ten mały kod do oauthWindow.html

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">
      if (window.opener && window.opener.oauth2 && window.opener.oauth2.__doLogin) {
        window.opener.oauth2.__doLogin(location.hash);
      } else {
        document.body.innerText = "Your browser seems to be stopping this window from communicating with the main window.";
      }
    </script>
  </head>
  <body></body>
</html>

Idealny!

Teraz, jeśli chcesz pracować z prywatnymi tabelami, wystarczy dodać znacznik access_token do adresu URL.

Dziękuję, że podałeś mi powód, by odpowiedzieć sobie!

questionAnswers(1)

yourAnswerToTheQuestion