私は不満を感じており、助けを求めています。 Oauth 2.0経由の認証が必要なgoolge fusion tables APIの使い方を調べるために、週末に新しいことを学びました。私はPHPで開発を始めたのは、私が道を邪魔するいくつかの例を見つけることができたからです。数日前に私はこの分野ではごくわずかしか知りませんでした。なぜ私がいくつかのアプローチの代わりに私のコードで特定のアプローチを試したのかどうか疑問に思っているのですが、単純な答えはこれがすべて私が見つけたことです。Oauth 2.0アクセストークンをPHPのFusion Tables APIに渡すときに無効な資格情報エラー
私は自分の個人プロフィールにアクセスするためにGoogleからのコード応答を求めるページを開発することに成功しました。また、コードを取得し、Googleに渡してアクセストークンとリフレッシュトークンを要求するリダイレクト先のページを正常に開発できました。このトークンは、ウェブサイトのパスワードで保護されたディレクトリにファイルに書き込まれました。また、更新トークンをGoogleに渡して更新されたアクセストークンを受け取って、必要に応じてそれをファイルに書き込むページを正常に作成することもできました。これらのすべては、PHPで行われました。
これで私の主な目的は、新しい融合テーブルに融合テーブルスタイルを書くことができるようにすることです。私は現在、私のGoogle Docsアカウントに完全に開発されたフュージョンテーブル(適切なスタイリングと共に)を数多く持っています。私はまた完全にデータを開発したがまだスタイル付けされていないテストフュージョンテーブルをアップロードした。私は、既存の完全に開発されたFTからスタイリングを取得し、同じスタイルをこのテストFTに書き込むコードを単純に記述しようとしていました。
私は正常にスタイルのJSONオブジェクトを既存のFTから取得し、そのJSONオブジェクトを変更してテーブルIDプロパティをテストFTに変更し、JSONオブジェクトをテストFTに戻すことができました。スタイルに書き込まれます。
しかし、POST経由でテストFTを更新するためにGoogleにスタイリングデータを渡そうとすると、 "{"エラー ":" {"" domain ":" global " "、" reason ":" authError "、" message ":無効な資格情報、" locationType ":"ヘッダー "、"場所 ":" Authorization "}]、" code ":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>
私はまた別の方法を試しました。以下のようなポストフィールドのオプションを簡素化し、その後
$arrHeader = array('Content-Type'=>'application/json', 'access_token'=>'redactedAccessToken in the form of ya29.AHE...Rdw');
そして:私のようなPOSTのヘッダにアクセストークンを入れて試してみた
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonWrite);
これは、さまざまなエラーを返します。「{」 "エラー":{"ドメイン": "グローバル"、 "理由": "必須"、 "メッセージ": "ログイン必須"、 "locationType": "ヘッダー"、 "場所": " }}、 "code":401、 "message": "ログインが必要です"}} "
できれば助けてください。あなたの時間をありがとう。
ありがとうキャスリン。あなたはあなたの時間と専門知識にいつも寛大であり、本当に感謝しています。 –
私はキャスリンの2番目の提案をうまく使いました。私はそれを動作させるために1つの追加の変更を加える必要がありました。変更する必要がありました:値のペアから単一の文字列へのヘッダー配列の値。 (私は '=>'を取り出して、ヘッダーに真ん中にコロンが付いた単一の文字列を作った)。再度、感謝します。 –