PHP中的Google预测培训

时间:2011-11-22 14:27:18

标签: php authorization prediction

我正在尝试在PHP中使用Google预测。我已经在OAuth 2.0中进行了授权,但现在我想训练我的模型。我正在尝试按照预测资源管理器中的示例(http://code.google.com/apis/explorer/#_s=prediction&_v=v1.4&_m=trainedmodels.insert),所以在PHP中我会喜欢这样:

$authCode = $_REQUEST['token'];
$id = $_REQUEST['id'];
$storageDataLocation = $_REQUEST['storageDataLocation'];
$api_key = "AIzaSyAIs5*************************";

$url =  "https://www.googleapis.com/prediction/v1.4/trainedmodels?pp=1&key=".$api_key;

$header = array('Content-Type:application/json',
                       'Authorization: OAuth = '.$authCode,
                       'X-JavaScript-User-Agent:  Google APIs Explorer google-api-gwt-client/0.1-alpha');


$success = "";


$data = array($id, $storageDataLocation);

$jsonData = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$ss = curl_exec($ch);
print_r($ss);

$response = json_decode();

if(!$response->{'data'}->{'data'}==$id){
           $success = $response->{'data'}->{'data'};
}
error_log("Training invocation result: ". $success);
echo $success;

那个print_r($ ss)给了我这个:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

我哪里错了?

0 个答案:

没有答案