Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何建立谷歌&x2B;登录网站的服务器端应用程序?_Php_Google Plus - Fatal编程技术网

Php 如何建立谷歌&x2B;登录网站的服务器端应用程序?

Php 如何建立谷歌&x2B;登录网站的服务器端应用程序?,php,google-plus,Php,Google Plus,我试图在我的网站上添加一个登录google+按钮来检索基本信息。 但是这些文档对我来说似乎没有任何意义。 () 它似乎过时了,不完整,而且似乎有各种api库可以使用 有没有人能更清楚地解释这一切,或者告诉我应该如何进行这项工作,以及使用哪个api库等? 带代码的完整示例将非常有用 塔克斯 好的,我将添加更多细节。google开发页面将此作为登录按钮的示例: <html> <head> <!-- BEGIN Pre-requisites --> &l

我试图在我的网站上添加一个登录google+按钮来检索基本信息。 但是这些文档对我来说似乎没有任何意义。 () 它似乎过时了,不完整,而且似乎有各种api库可以使用

有没有人能更清楚地解释这一切,或者告诉我应该如何进行这项工作,以及使用哪个api库等? 带代码的完整示例将非常有用

塔克斯

好的,我将添加更多细节。google开发页面将此作为登录按钮的示例:

<html>

<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 -->
</head>
<body>
<div id="signinButton">
  <span class="g-signin"
    data-scope="https://www.googleapis.com/auth/plus.login"
    data-clientid="your-client-id"
    data-redirecturi="postmessage"
    data-accesstype="offline"
    data-cookiepolicy="single_host_origin"
    data-callback="signInCallback">
  </span>
</div>
<div id="result"></div>
<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: 'plus.php?storeToken',
      contentType: 'application/octet-stream; charset=utf-8',
      success: function(result) {
        // Handle or verify the server response if necessary.

        // Prints the list of people that the user has allowed the app to know
        // to the console.
        console.log(result);
        if (result['profile'] && result['people']){
          $('#results').html('Hello ' + result['profile']['displayName'] + '. You successfully made a server side call to people.get and people.list');
        } else {
          $('#results').html('Failed to make a server-side call. Check your configuration and console.');
        }
      },
      processData: false,
      data: authResult['code']
    });
  } else if (authResult['error']) {
    // There was an error.
    // Possible error codes:
    //   "access_denied" - User denied access to your app
    //   "immediate_failed" - Could not automatially log in the user
    // console.log('There was an error: ' + authResult['error']);
  }
}
</script>

</body>
</html>

函数signInCallback(authResult){
if(authResult['code']){
//现在用户已获得授权,请隐藏“登录”按钮,例如:
$('signinButton').attr('style','display:none');
//将代码发送到服务器
$.ajax({
键入:“POST”,
url:'plus.php?storeToken',
contentType:'应用程序/八位字节流;字符集=utf-8',
成功:功能(结果){
//如有必要,处理或验证服务器响应。
//打印用户允许应用程序认识的人的列表
//到控制台。
控制台日志(结果);
如果(结果['profile']&结果['people']){
$('#results').html('Hello'+result['profile']['displayName']+'。您成功地对people.get和people.list'进行了服务器端调用);
}否则{
$('#results').html('未能进行服务器端调用。请检查您的配置和控制台');
}
},
processData:false,
数据:authResult['code']
});
}else if(authResult['error']){
//有一个错误。
//可能的错误代码:
//“拒绝访问”-用户拒绝访问您的应用
//“立即\u失败”-无法自动登录用户
//log('出现错误:'+authResult['error']);
}
}
但它还规定:

<?php 
// Create a state token to prevent request forgery.
  // Store it in the session for later validation.
  $state = md5(rand());
  $app['session']->set('state', $state);
  // Set the client ID, token state, and application name in the HTML while
  // serving it.
  return $app['twig']->render('index.html', array(
      'CLIENT_ID' => CLIENT_ID,
      'STATE' => $state,
      'APPLICATION_NAME' => APPLICATION_NAME
  ));
   // Ensure that this is no request forgery going on, and that the user
  // sending us this connect request is the user that was supposed to.
  if ($request->get('state') != ($app['session']->get('state'))) {
    return new Response('Invalid state parameter', 401);
  }
  $code = $request->getContent();
  $gPlusId = $request->get['gplus_id'];
  // Exchange the OAuth 2.0 authorization code for user credentials.
  $client->authenticate($code);

  $token = json_decode($client->getAccessToken());
  // Verify the token
  $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' .
          $token->access_token;
  $req = new Google_HttpRequest($reqUrl);

  $tokenInfo = json_decode(
      $client::getIo()->authenticatedRequest($req)->getResponseBody());

  // If there was an error in the token info, abort.
  if ($tokenInfo->error) {
    return new Response($tokenInfo->error, 500);
  }
  // Make sure the token we got is for the intended user.
  if ($tokenInfo->userid != $gPlusId) {
    return new Response(
        "Token's user ID doesn't match given user ID", 401);
  }
  // Make sure the token we got is for our app.
  if ($tokenInfo->audience != CLIENT_ID) {
    return new Response(
        "Token's client ID does not match app's.", 401);
  }

  // Store the token in the session for later use.
  $app['session']->set('token', json_encode($token));
  $response = 'Succesfully connected with token: ' . print_r($token, true);

?>

好的,如果其他人有问题。

我从那里下载了api库,更改了配置文件,并使用了提供的示例,效果很好

要使其在本地主机上工作,您必须将您的授权JavaScript源代码设置为本地主机:#例如
http://localhost:12345

然后,要使浏览器在命令提示下接受文件夹或登录页面,请键入

cd c:/the/path/of/the/download/api/example

然后输入:

php -S localhost:12345

希望这能帮助大家

有人知道为什么我在这方面得了-1分吗?对我来说似乎是个合理的问题链接断了