0
我試圖讓Google Plus登錄Laravel 4(MVC框架)。
我現在有兩種方法home
和connect
:
public function home()
{
$state = md5(rand());
Session::put('state', $state);
$data = array (
'CLIENT_ID' => 'my client id',
'STATE' => $state,
'APPLICATION_NAME' => 'API Test'
);
return View::make('home')
->with($data);
}
這將創建變量主要有「家」的看法。
在這裏我有所需的腳本,用我的按鈕:
<div id="test"></div>
<div id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login"
data-clientid="{{ $CLIENT_ID }}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
這然後加載我signInCallback功能:
function signInCallback(authResult) {
if (authResult['code']) {
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
var dataString = 'auth= ' +authResult['code'];
// Send the code to the server
$.ajax({
type: 'POST',
url: 'connect',
dataType: 'html',
data: dataString,
processData: false,
statusCode: {
200: function(result){
$('#test').html(result);
}
},
});
} else if (authResult['error']) {
console.log('There was an error: ' + authResult['error']);
}
}
所有這一切工作正常。
我的連接方法如何接收AUTHCODE,我想爲所需要的輔幣這樣的:
public function connect()
{
$client = new Google_Client();
$client->setApplicationName('API Test');
$client->setClientId('my client id');
$client->setClientSecret('my client secret');
$client->setRedirectUri('postmessage');
// Get auth code - this works
$code = Input::get('auth');
$client->authenticate($code);
$token = json_decode($client->getAccessToken());
return "Token: $token";
}
然而,這會返回一個500內部服務器錯誤 - 當我添加$client->authenticate($code);
它發生。任何人都可以幫忙嗎?我發現文檔非常混亂。
500錯誤來自Google服務器還是來自您的應用?我的理解是,錯誤來自您的應用程序 - 您是否可以檢查日誌並提供有關錯誤的更多詳細信息? – Joanna