0

我的问题与我之前的问题有关,我认为我已经解决了这个问题。我已经能够使用我找到的脚本获得一个 refresh_token - 包括我上一个问题中的那些。但是现在有了这个刷新令牌,我现在尝试做一些简单的事情,首先列出我的测试日历中的事件

我的问题是尝试使用此 URL 列出事件会返回错误 - trying to get property of non object。这与这行代码有关:

$cAccessToken = $oToken->access_token;

所以我认为获取访问令牌有问题。

在我获得添加此代码所需的代码的脚本部分中:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

这产生了令牌 - 所以我将这一行添加到再次获取访问令牌的代码中,但它只是产生了一个空白页面。

我必须承认对 google 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 游乐场,可以在那里得到结果,但不能用我自己的代码。顺便说一句,我使用了不同的代码,因为我认为它们应该不同?

4

0 回答 0