PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将Oauth 2.0访问令牌传递给PHP中的Fusion Tables API时出现无效凭证错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我已经达到沮丧的境地,正在寻求帮助.我花了整个周末学习新事物,以尝试弄清楚如何使用需要通过Oauth 2.0进行身份验证的goolge融合表API.我开始使用PHP进行开发仅仅是因为我能够找到一些帮助我前进的示例.几天前,我对该领域了解甚少,如果您想知道为什么我尝试在下面的代码中尝试某种方法而不是其他方法,那么简单的答案就是我找到了所有这些.

我能够成功开发一个页面,该页面将征求Google的代码响应,以访问我自己的个人资料.我还能够成功开发一个位于所需重定向位置的页面,该页面将采用该代码,将其传递回google并请求访问令牌和刷新令牌,它们已成功写入文件到我网站上受密码保护的目录中.我还能够成功开发一个页面,该页面将刷新令牌传递回google,并接收更新的访问令牌并将其写入文件(如果需要).所有这些都是在PHP中完成的.

现在,我所有这些的主要目标是能够将融合表样式写入新的融合表.目前,我的google文档帐户中有许多完全开发的融合表(以及正确的样式).我还上传一个测试融合表,该表具有完全开发的数据,但尚未设置样式.我试图简单地编写一些代码,这些代码将从现有的完全开发的FT中获取样式,并将相同的样式写入此测试FT中.

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

但是,当我尝试将样式数据传递回google以通过POST更新测试FT时,我得到一个错误,该错误打印为“ {” error“:{” errors“:[{” domain“:” global“,” reason “:” authError“,” message“:”无效的凭证“,” locationType“:”标题“,” location“:”授权“}],”代码“:401,” message“:”无效的凭证“}}”

我很可能会错误地格式化POST.但是,到目前为止,我对访问令牌还没有做任何花哨的事情.我只是发出了请求,从Google收到了请求,然后将其复制并直接粘贴到代码中,因此在分配变量和/或超时方面不会有问题.

这是我希望成为一个非常直接的过程的代码,其中某些敏感数据项已被删除但已描述.如果可以的话,请提供建议.

<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);

这将返回不同的错误:“ {”错误“:{”错误“:[{”域“:”全局“,”原因“:”必需“,”消息“:”需要登录“,” locationType“:”标题“,”位置“:”授权“}],”代码“:401,”消息“:”需要登录“}}”

如果可以的话,请提供任何帮助.感谢您的时间.

解决方法:

访问令牌应作为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>');

大佬总结

以上是大佬教程为你收集整理的将Oauth 2.0访问令牌传递给PHP中的Fusion Tables API时出现无效凭证错误全部内容,希望文章能够帮你解决将Oauth 2.0访问令牌传递给PHP中的Fusion Tables API时出现无效凭证错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: