2014-10-05 118 views
0

我正在尝试添加用我的网站上的Google +按钮登录以检索基本信息。 但文档似乎对我没有任何意义。 (https://developers.google.com/+/web/signin/server-side-flow) 它看起来过时并且不完整,似乎有可用的各种api库。如何为网站的服务器端应用设置Google+登录?

任何人都可以更清楚地解释所有这些或告诉我应该如何去做这项工作以及哪个API库使用等?带代码的完整示例将非常有帮助。

thanx

好吧,我会添加更多的细节。谷歌开发页面给这个作为一个登录按钮的例子:

<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> 

而且还提供:

<?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库或在哪里放置秘密或任何东西。所以我可以做一些指向正确的方向吗?

+0

有人知道为什么我给了-1这个?对我来说似乎是一个有效的问题 – dragonvsphoenix 2014-10-05 20:32:48

回答

1

好吧,如果有其他人有麻烦。 i followed the tutorial on this link

我从那里下载了api库,更改了configs文件并使用了提供的示例,并且它工作正常。

,使其对你有设置你的授权的JavaScript源到本地主机本地主机的工作:例如#http://localhost:12345

然后让你的浏览器接受的文件夹或登录页面在命令提示符下键入

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

然后键入:

php -S localhost:12345 

希望对任何人有所帮助

相关问题