1

我正在尝试构建几个网页来收集有关我们 GApps 客户的信息。

我拥有获取 Oauth 令牌所需的所有代码,它甚至似乎可以工作(至少我可以使用日历 API 读取我的所有日​​历事件)。当我尝试访问经销商 api 时,我得到了令牌,但是如果我尝试访问userinfo,传递 access_token 参数我得到一个Invalid Credentials错误。

不幸的是,我还没有找到太多关于 PHP 的可用文档,但是在完成所有身份验证程序之后,我试图通过 CURL 获取数据......

有什么想法有什么问题或我应该做些什么不同的事情吗?


仍然卡住了,所以我想我会添加我现在正在运行的代码:

<?php
require_once '../google-api-php-client/src/Google_Client.php';
require_once '../google-api-php-client/src/contrib/Google_Oauth2Service.php';
require_once '../google-api-php-client/src/contrib/Google_ResellerService.php';
session_name('reseller');
session_start();

$debug=1;

$clientId='******.apps.googleusercontent.com';
$clientSecret='******';
$redirectURI='*****'; //links to the directory wehere the script is
$key='********';
$scopes='https://www.googleapis.com/auth/apps.order.readonly';
n
$client = new Google_Client();
$client->setApplicationName("Google Reseller PHP Starter Application");

$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectURI);
$client->setDeveloperKey($key);
$client->setScopes($scopes);

if (isset($_GET['code'])) {
if($debug)  echo "we have a code! <br />";

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  //echo $_SESSION['token']."<br/>";
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  exit();
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
echo "access token settato<br/>";
}

if ($client->getAccessToken()) {
  $mytoken=json_decode($_SESSION['token'], true);

  echo "DEBUG SESSION token:".$_SESSION['token']."<br />";
  echo "DEBUG token: ".$mytoken['access_token']."<br />";

  $domain='******'; //mydomain
  //key id defiend above
  $fields=array('customerId');
  $url="https://www.googleapis.com/oauth2/v1/userinfo/?access_token=".$mytoken['access_token'];

  echo $url."<br />";

  $ch = curl_init($url);
  $scopes&state=antani&access_type=online&approval_prompt=auto");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer: '.$mytoken['access_token'],
    ));
  curl_setopt($ch, CURLOPT_REFERER, "*******"); //server URL
  $data = curl_exec($ch);
  curl_close($ch);

 $resellerService = new Google_ResellerService($client);

if ($debug){
echo "<pre>";
  print_r($data);
echo "</pre>";
}


} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}



if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}


session_destroy();
?>

关于为什么我仍然收到“权限不足”错误的任何想法?

谢谢!

4

0 回答 0