将Oauth 2.0访问令牌传递到PHP中的Fusion Tables API时出现无效凭据错误


Invalid Credentials Error when passing Oauth 2.0 Access Token to Fusion Tables API in PHP

我已经到了沮丧的地步,正在寻求帮助。我花了整整一个周末的时间学习新东西,试图弄清楚如何使用需要通过Oauth 2.0进行身份验证的googlege融合表API。我开始用php进行开发,只是因为我能够找到一些帮助我走上这条道路的例子。在几天前,我对这一领域知之甚少,如果你想知道为什么我在下面的代码中尝试了某种方法,而不是其他方法,简单的答案是,这就是我所找到的。

我成功地开发了一个页面,该页面将向谷歌征求代码响应,以便访问我自己的个人资料。我还成功地开发了一个位于所需重定向位置的页面,该页面将接收该代码,将其传递回谷歌,并请求访问令牌和刷新令牌,这些令牌已成功写入我网站上受密码保护的目录中的文件。我还成功地开发了一个页面,该页面将刷新令牌传递回谷歌,并接收更新的访问令牌,并在必要时将其写入文件。所有这些都是在php中完成的。

现在,我的主要目标是能够将融合表样式写入到新的融合表中。我目前在我的谷歌文档帐户中有许多完全开发的融合表(以及适当的样式)。我还上传了一个测试融合表,该表已经完全开发了数据,但尚未定型。我试图简单地编写一些代码,从现有的完全开发的FT中获得样式,并将相同的样式写入这个测试FT中。

我已经能够成功地从现有的FT中获得样式JSON对象,并修改该JSON对象,使其表id属性更改为测试FT,以便JSON对象可以传递回测试FT并写入样式中。

然而,当我试图通过POST将样式数据传递回谷歌以更新测试FT时,我得到了一个错误,该错误打印为"{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header"

完全有可能我的POST格式不正确。然而,到目前为止,我还没有对访问令牌做任何花哨的事情。我只是提出了请求,从谷歌收到它,并将其直接复制粘贴到代码中,这样就不会出现分配变量和/或超时的问题。

以下是我的代码,我希望这是一个非常简单的过程,对某些敏感数据项进行了编辑但进行了描述。如果可以的话,请提供建议。

<html>
<head>
<title>Google Fusion Tables API Example</title>
</head>
<body>
<?php
  //table id of the FT that has fully developed styling.
  $strTableID = '1r8CRtGalQ3vC0zd_xmsChzjALlriiV5UDYFVOJU';
  //prepare your cURL request for GET of FT styling data from existing table.  Initialize it, set the options and then execute it.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/fusiontables/v1/tables/'.$strTableID.'/styles/1?&key=redactedKey');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);
  //print result to see if it works (it works)
  echo "<br> Result: $result";
  //the result returns as a JSON object.  Decode the object into a standard array, then display the individual elements of the array to see that it worked.
  $jsonResult = json_decode($result,true);
  echo "<br><br> JSON Result: ".$jsonResult['polygonOptions']['fillColorStyler']['columnName'];
  echo "<br> Table ID: ".$jsonResult['tableId'];
  //update the tableID and the bound column
  //this is the table id of the test FT
  $strTableID = '1CnecsDL67YHjBzSmfLjODRWxHDuC39frZEaTEKQ';
  $jsonResult['tableId'] = $strTableID;
  $jsonResult['polygonOptions']['fillColorStyler']['columnName'] = 'redacted column name';
  //print out the updated new JSON elements to see that they were properly applied (works)
  echo "<br><br> JSON Result: ".$jsonResult['polygonOptions']['fillColorStyler']['columnName'];
  echo "<br> Table ID: ".$jsonResult['tableId'];
  //Re-encode the array into a JSON object
  $jsonWrite = json_encode($jsonResult);

  $postField = 'access_token=redactedAccessToken in the form of ya29.AHE...Rdw';
  //set your header type so that Google knows what type of data it is receiving
  $arrHeader = array('Content-Type'=>'application/json');
  $url = "https://www.googleapis.com/fusiontables/v1/tables/".$strTableID."/styles";
  //prepare your cURL request.  Initialize it, set the options and then execute it.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postField.'&'.$jsonWrite);
  $result = curl_exec($ch);
  curl_close($ch);
  //print out the response to see if it worked (doesn't work)
  echo "<br><br>Post Result: $result";
?>
</body>
</html>

我还尝试了一种替代方法。我已经尝试将访问令牌放入POST的头部,如下所示:

$arrHeader = array('Content-Type'=>'application/json', 'access_token'=>'redactedAccessToken in the form of ya29.AHE...Rdw');

然后简化后字段选项,如:

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonWrite);

这返回一个不同的错误:"{"error":{"errors":[{"domain":"global","reason":"required","message":"Login required","locationType":"header","location":"Authorization"}],"code":401,"message":"LoginRequired"}}"

如果可以的话,请提供任何帮助。谢谢你抽出时间。

访问令牌应作为Authorization:Bearer HTTP标头发送,或作为URL中access_token参数的值发送。

要作为Authorization:Bearer标头发送,请再次使用$arrHeader变量,但将代码行更新为以下内容:

$arrHeader = array('Content-Type'=>'application/json', 'Authorization'=>'Bearer <your_access_token>');

或者,如果您想使用access_token参数,请更新以下行,不要在帖子正文中发送访问令牌:

  curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/fusiontables/v1/tables/'.$strTableID.'/styles/1?&key=redactedKey&access_token=<your_access_token>');