我已经达到了沮丧的地步,正在寻求帮助。我整个周末都在学习新东西,以便尝试弄清楚如何使用需要通过 Oauth 2.0 进行身份验证的 goolge fusion table API。我开始使用 php 进行开发只是因为我能够找到一些帮助我走上这条道路的示例。几天前,我对这个领域知之甚少,如果您想知道为什么我在下面的代码中尝试某种方法而不是其他方法,简单的答案是这就是我发现的全部。
我成功地开发了一个页面,该页面可以请求 Google 的代码响应来访问我自己的个人资料。我还能够成功开发位于所需重定向位置的页面,该页面将获取该代码,将其传回谷歌并请求访问令牌和刷新令牌,这些令牌已成功写入我网站上受密码保护的目录中的文件。我还能够成功开发一个页面,将刷新令牌传递回谷歌并接收更新的访问令牌并将其写入文件(如果/在必要时)。所有这些都是在 php 中完成的。
现在,我所有这一切的主要目标是能够将融合表样式写入新的融合表。我目前在我的谷歌文档帐户中有许多完全开发的融合表(以及正确的样式)。我还上传了一个测试融合表,该表已完全开发数据但尚未设计样式。我试图简单地编写一些代码,从现有的完全开发的 FT 中获取样式,并将相同的样式写入此测试 FT 中。
我已经能够从现有 FT 成功获取样式 JSON 对象并修改该 JSON 对象,使其表 id 属性更改为测试 FT,以便可以将 JSON 对象传递回测试 FT 并写入样式。
但是,当我尝试将样式数据传回 google 以通过 POST 更新测试 FT 时,我收到一个错误,打印为 " { "error": { "errors": [ { "domain": "global", "reason ": "authError", "message": "无效凭证", "locationType": "header", "location": "授权" } ], "code": 401, "message": "无效凭证" } }"
完全有可能我的 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 ", "位置": "授权" } ], "代码": 401, "消息": "需要登录" } }"
如果可以的话,请提供任何帮助。感谢您的时间。