我的问题与我之前的问题有关,我认为我已经解决了这个问题。我已经能够使用我找到的脚本获取refresh_token - 包括我之前的问题中的脚本。 但是现在手持这个刷新令牌,我现在正试图在第一个实例中做一些简单的事情列出我的测试日历中的事件
我的问题是尝试使用此网址列出事件会返回错误 - trying to get property of non object
。这涉及到这行代码:
$cAccessToken = $oToken->access_token;
所以我认为获取访问令牌有问题。
在我的脚本部分中,我获取了代码,我需要添加此代码:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
这产生了令牌 - 所以我将这一行添加到获取访问令牌的代码中,但它只是生成了一个空白页面。
我必须承认与谷歌API非常混淆。我尝试使用google-client-api代码,但也不太明白 - 我一直跑进砖墙。
这是我的代码:
<?php
$cPublisherID = '';
$cScope = 'https://www.google.com/calendar/feeds/';
$cClientID = 'XXXXXX.apps.googleusercontent.com';
$cClientSecret = 'XXXXXX';
$cRedirectURI = 'XXXXXX';
$cAuthCode = 'XXXXXX';
$cRefreshToken = 'XXXXXX';
$bRefresh = true;
if (empty($cAuthCode)) {
$rsParams = array(
'response_type' => 'code',
'client_id' => $cClientID,
'redirect_uri' => $cRedirectURI,
'scope' => $cScope
);
$cOauthURL = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($rsParams);
echo("Go to\n$cOauthURL\nand enter the given value into \$cAuthCode\n");
exit();
} // ends if (empty($cAuthCode))
elseif (empty($cRefreshToken)) {
$cTokenURL = 'https://accounts.google.com/o/oauth2/token';
$rsPostData = array(
'code' => $cAuthCode,
'client_id' => $cClientID,
'client_secret' => $cClientSecret,
'redirect_uri' => $cRedirectURI,
'grant_type' => 'authorization_code',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cTokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$cTokenReturn = curl_exec($ch);
$oToken = json_decode($cTokenReturn);
echo("Enter the following values:\n\n");
echo("\$cRefreshToken = '" . $oToken->refresh_token . "';\n");
} // ends
else {
// Get a new Access Token
$cTokenURL = 'https://accounts.google.com/o/oauth2/token';
$rsPostData = array(
'client_secret' => $cClientSecret,
'grant_type' => 'refresh_token',
'refresh_token' => $cRefreshToken,
'client_id' => $cClientID
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cTokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$cTokenReturn = curl_exec($ch);
$oToken = json_decode($cTokenReturn);
$cAccessToken = $oToken->access_token;
// Get the results
//$cGANURL = 'https://www.googleapis.com/gan/v1beta1/publishers/' . $cPublisherID . '/events';
$rest_url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events';
$cAuthHeader = "Authorization: OAuth " . $cAccessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array($cAuthHeader));
curl_setopt($ch, CURLOPT_URL, $rest_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cJsonReturn = curl_exec($ch);
print_r(json_decode($cJsonReturn));
} // ends else from <all authenticated>
?>
我真的不知道解决这个问题 - 所以基本上我认为我得到了我需要的令牌,包括刷新令牌,但不知道如何测试和理解发生了什么。
我已经使用了oauth游乐场并且可以在那里获得结果,但不能使用我自己的代码。我假设它们应该是不同的,我使用了不同的代码?