简体   繁体   English

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

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

I've reached my frustration point and am asking for help. 我已达到沮丧的程度,并寻求帮助。 I spent all weekend learning new things in order to try and figure out how to use the goolge fusion tables API which requires authentication via Oauth 2.0. 我花了整个周末学习新东西,试图找出如何使用goolge融合表API,这需要通过Oauth 2.0进行身份验证。 I started developing in php solely because I was able to find some examples that helped me down the path. 我开始在php开发只是因为我能够找到一些帮助我走上正轨的例子。 Prior to a few days ago I knew very little in this area and if you're wondering why I tried a certain approach in my code below instead of some other approach the simple answer is that this is all I found. 在几天之前,我对这个领域知之甚少,如果你想知道为什么我在下面的代码中尝试某种方法而不是其他方法,简单的答案就是这就是我发现的。

I was able to successfully develop a page that would solicit a code response from Google for access to my own personal profile. 我能够成功开发一个页面,该页面将征求Google的代码回复,以访问我自己的个人资料。 I was also able to successfully develop a page located at the required redirect location that would take that code, pass it back to google and solicit an access token and a refresh token, which were successfully written to file in a password protected directory on my website. 我还能够成功开发位于所需重定向位置的页面,该页面将获取该代码,将其传递回google并请求访问令牌和刷新令牌,这些令牌已成功写入我网站上受密码保护的目录中的文件。 I was also able to successfully develop a page that would pass the refresh token back to google and receive an updated access token and write that to file, if/when necessary. 我还能够成功开发一个页面,将刷新令牌传递回谷歌并接收更新的访问令牌,并在必要时将其写入文件。 All of this was done in php. 所有这些都是在php中完成的。

Now my primary objective for all of this is to be able to write fusion table styles to new fusion tables. 现在,我所有这一切的主要目标是能够将融合表样式编写到新的融合表中。 I currently have a number of fully developed fusion tables (along with the proper styling) in my google docs account. 我目前在谷歌文档帐户中有许多完全开发的融合表(以及正确的样式)。 I also uploaded a test fusion table that has fully developed data but has not yet been styled. 我还上传了一个测试融合表,该表已经完全开发了数据但还没有设计样式。 I was attempting to simply write some code that would get the styling from an existing fully developed FT and write the same style into this test FT. 我试图简单地编写一些代码,这些代码可以从现有的完全开发的FT中获得样式,并将相同的样式写入此测试FT。

I have been able to successfully get the style JSON object from the existing FT and modify that JSON object such that it's table id property is changed to the test FT so that the JSON object may be passed back to the test FT and written into the styles. 我已经能够从现有FT成功获取样式JSON对象并修改该JSON对象,使其表id属性更改为测试FT,以便JSON对象可以传递回测试FT并写入样式。

However when I try to pass back the styling data to google to update the test FT via POST I get an error that prints out to " { "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }" 然而,当我尝试将样式数据传回谷歌以通过POST更新测试FT时,我得到一个错误,打印出“{”错误“:{”errors“:[{”domain“:”global“,”reason“ “:”authError“,”message“:”无效凭据“,”locationType“:”标题“,”位置“:”授权“}],”代码“:401,”消息“:”无效凭据“}}”

It's entirely possible that I am formatting the POST incorrectly. 我错误地格式化POST完全有可能。 However, I am not doing anything fancy (so far) with the access token. 但是,我没有使用访问令牌做任何花哨的事情(到目前为止)。 I've simply made the request, received it from google and copied and pasted it directly into the code so there could be no issues on assigning variables and/or timing out. 我只是提出了请求,从谷歌收到它并将其复制并直接粘贴到代码中,因此在分配变量和/或超时时可能没有问题。

Here is my code for what I had hoped to be a pretty straightforward process, with certain sensitive data items redacted but described. 这是我的代码,我希望这是一个非常简单的过程,某些敏感数据项被编辑但描述。 Please offer suggestions if you can. 如果可以,请提供建议。

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

I have also tried one alternative method. 我也尝试了一种替代方法。 I've tried putting the access token into the header of the POST as such: 我已经尝试将访问令牌放入POST的标题中:

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

And then simplifying the Post Fields options as such: 然后简化Post Fields选项:

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonWrite);

This returns a different error: "{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }" 这会返回一个不同的错误:“{”error“:{”errors“:[{”domain“:”global“,”reason“:”required“,”message“:”Login Required“,”locationType“:”header “,”“location”:“授权”}],“代码”:401,“消息”:“需要登录”}}“

Please provide any assistance if you can. 如果可以,请提供任何帮助。 Thank you for your time. 感谢您的时间。

The access token should either sent as an Authorization: Bearer HTTP header OR as the value of access_token parameter in the URL. 访问令牌应作为授权:Bearer HTTP标头或URL中的access_token参数的值发送。

To send as the Authorization: Bearer header, use the $arrHeader variable again, but update the line of code to the following: 要作为Authorization:Bearer标头发送,请再次使用$ arrHeader变量,但将代码行更新为以下内容:

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

Or, if you want to use the access_token parameter, update the following line and do not send the access token in the body of the post: 或者,如果要使用access_token参数,请更新以下行,并且不要在帖子正文中发送访问令牌:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:Google plus Oauth 2.0 - 获取OAuth2访问令牌时出错,消息:&#39;invalid_client&#39; - PHP : Google plus Oauth 2.0 - Getting Error fetching OAuth2 access token, message: 'invalid_client' 使用PHP cURL获取access_token时,Google OAuth2.0返回“ invalid_request” - Google OAuth2.0 returns 'invalid_request' when getting access_token with PHP cURL 使用oauth 2.0 google + api烦人的无效凭据 - Annoying invalid credentials with oauth 2.0 google + api 错误:FacebookApiException [190]:无效的OAuth 2.0访问令牌 - ERROR: FacebookApiException [ 190 ]: Invalid OAuth 2.0 Access Token 带有PHP服务帐户的Google OAuth 2.0:“无效的令牌格式错误” - Google OAuth 2.0 with Service account with PHP: “Invalid token format error” [PHP]使用Google api oauth访问令牌时出现错误401令牌无效 - [PHP]Error 401 Token invalid while using google api oauth access token Facebook Ad API PHP:无效的OAuth访问令牌 - Facebook Ad API PHP : Invalid OAuth access token Facebook PHP SDK - 图形返回错误:无效的 OAuth 访问令牌 - Facebook PHP SDK - Graph returned an error: Invalid OAuth access token 获取Spotify API OAuth 2.0 token with PHP - Obtain Spotify API OAuth 2.0 token with PHP Linkedin REST API:使用CRON作业中的php更新OAuth2.0访问令牌吗? - Linkedin REST API: Update OAuth2.0 Access token using php from a CRON job?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM