Google старый API данных профиля не работает, что такое новый URL API? - PullRequest
0 голосов
/ 17 марта 2019

API старого профиля Google не работает. Я использовал эту ссылку API раньше, (http://picasaweb.google.com/data/entry/api/user/sakiremail@gmail.com?alt=json) но это не работает сейчас. Не могу получить информацию о профиле. Что такое новый URL API?

1 Ответ

0 голосов
/ 17 марта 2019

здесь я создал демонстрационное приложение для работы. Перед тестом создайте идентификатор клиента авторизации. Кроме того, добавьте авторизованный URI перенаправления на http://localhost, если вы проверяете локальный.

<html lang="en">
  <head>
    <meta name="google-signin-scope" content="profile email">
  </head>
  <body>
    <a href="javascript:" onclick="oauthSignIn()">Login</a>
    <script>
      /*
	 * Create form to request access token from Google's OAuth 2.0 server.
	 */
	function oauthSignIn() {
	  // Google's OAuth 2.0 endpoint for requesting an access token
	  var oauth2Endpoint = 'https://accounts.google.com/o/oauth2/v2/auth';

	  // Create <form> element to submit parameters to OAuth 2.0 endpoint.
	  var form = document.createElement('form');
	  form.setAttribute('method', 'GET'); // Send as a GET request.
	  form.setAttribute('action', oauth2Endpoint);

	  // Parameters to pass to OAuth 2.0 endpoint.
	  var params = {'client_id': 'YOUR_APP_CLIENT_ID',
					'redirect_uri': 'AUTHENTICATED_REDIRECT_URI',
					'response_type': 'token',
					'scope': 'profile',
					'include_granted_scopes': 'true',
					'state': 'pass-through value'};

	  // Add form parameters as hidden input values.
	  for (var p in params) {
		var input = document.createElement('input');
		input.setAttribute('type', 'hidden');
		input.setAttribute('name', p);
		input.setAttribute('value', params[p]);
		form.appendChild(input);
	  }

	  // Add form to page and submit it to open the OAuth 2.0 endpoint.
	  document.body.appendChild(form);
	  form.submit();
	}
	
	var hash = window.location.hash.substr(1);

	var hashresult = hash.split('&').reduce(function (result, item) {
		var parts = item.split('=');
		result[parts[0]] = parts[1];
		return result;
	}, {});
	
	if(hashresult.access_token){
		console.log(hashresult.access_token);
		fetch('https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='+hashresult.access_token)
		.then(function(response) {
			return response.json();
		})
		.then(function(userdata) {
		console.log(userdata);
		});
	}
	
    </script>
	
  </body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...