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.
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.
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.
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.
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" } }"
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>
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);
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.
The access token should either sent as an Authorization: Bearer HTTP header OR as the value of access_token parameter in the URL.
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>');
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>');
来源:https://stackoverflow.com/questions/9576733/invalid-credentials-error-when-passing-oauth-2-0-access-token-to-fusion-tables-a