将Oauth 2.0访问令牌传递给PHP中的Fusion Tables API时出现无效凭证错误 [英] Invalid Credentials Error when passing Oauth 2.0 Access Token to Fusion Tables API in PHP

查看:68
本文介绍了将Oauth 2.0访问令牌传递给PHP中的Fusion Tables API时出现无效凭证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经达到沮丧的境地,正在寻求帮助.我花了整个周末学习新事物,以尝试弄清楚如何使用需要通过Oauth 2.0进行身份验证的goolge融合表API.我开始使用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. I started developing in php solely because I was able to find some examples that helped me down the path. 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.

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

I was able to successfully develop a page that would solicit a code response from Google for access to my own personal profile. 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. 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.

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

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成功获取样式JSON对象,并修改该JSON对象,以便将其表id属性更改为测试FT,以便可以将JSON对象传递回测试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.

但是,当我尝试将样式数据传递回google以通过POST更新测试FT时,我得到一个错误,该错误会打印为"{" error:{" errors:[{" domain:" global",原因":"authError",消息":无效的凭据","locationType":标头",位置":授权"}],代码":401,消息":无效的凭据" }}"

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错误.但是,到目前为止,我对访问令牌没有做任何花哨的事情.我只是发出了请求,从Google收到了请求,然后将其复制并直接粘贴到代码中,因此在分配变量和/或超时方面不会有任何问题.

It's entirely possible that I am formatting the POST incorrectly. 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>

我也尝试了一种替代方法.我尝试将访问令牌这样放入POST的标头中:

I have also tried one alternative method. I've tried putting the access token into the header of the POST as such:

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

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonWrite);

这将返回不同的错误:"{" error:{" errors:[{" domain:" global," reason:" required," message:"需要登录," locationType":"header","location":授权"}],代码":401,"message":需要登录"}}"

This returns a different error: "{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }"

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

Please provide any assistance if you can. Thank you for your time.

推荐答案

访问令牌应作为授权:承载HTTP标头或作为URL中access_token参数的值发送.

The access token should either sent as an Authorization: Bearer HTTP header OR as the value of access_token parameter in the URL.

要以授权:承载"标头发送,请再次使用$ arrHeader变量,但将代码行更新为以下内容:

To send as the Authorization: Bearer header, use the $arrHeader variable again, but update the line of code to the following:

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

或者,如果要使用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:

  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时出现无效凭证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆