Google 与 PHP 一起玩开发者 API
Google Play Developer API with PHP
我想使用 PHP 和 Google Play 开发者 API 来保护和验证应用内购买免受假冒交易。为此,我查看了 Google 文档。我首先通过完成 link https://developers.google.com/android-publisher/authorization. Then i tried performing the actions described in this link https://developers.google.com/android-publisher/api-ref/purchases/products/get#auth 中的步骤创建了一个 access_token,但我失败了。我的代码和得到的结果如下。
我的代码:
<?php
$curl = curl_init();
$headers = array(
'access_token: my_access_token',
'expires_in: 3600',
'token_type: Bearer'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, "https://www.googleapis.com/androidpublisher/v2/applications/my.package.name/purchases/products/myProductId/tokens/myPurchaseToken");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec($curl);
echo $result;
curl_close($curl);
?>
结果:
{
error: {
errors: [
{
domain: "global",
reason: "required",
message: "Login Required",
locationType: "header",
location: "Authorization"
}
],
code: 401,
message: "Login Required"
}
}
我哪里做错了,我该怎么办?从现在开始谢谢你。
你不能给 header 这样的不记名类型令牌。你的代码应该是这样的:
<?php
$curl = curl_init();
$authorization = "Authorization: Bearer your_access_token";
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($curl, CURLOPT_URL, "https://www.googleapis.com/androidpublisher/v2/applications/my.package.name/purchases/products/myProductId/tokens/myPurchaseToken");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec($curl);
echo $result;
curl_close($curl);
?>
我想使用 PHP 和 Google Play 开发者 API 来保护和验证应用内购买免受假冒交易。为此,我查看了 Google 文档。我首先通过完成 link https://developers.google.com/android-publisher/authorization. Then i tried performing the actions described in this link https://developers.google.com/android-publisher/api-ref/purchases/products/get#auth 中的步骤创建了一个 access_token,但我失败了。我的代码和得到的结果如下。
我的代码:
<?php
$curl = curl_init();
$headers = array(
'access_token: my_access_token',
'expires_in: 3600',
'token_type: Bearer'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, "https://www.googleapis.com/androidpublisher/v2/applications/my.package.name/purchases/products/myProductId/tokens/myPurchaseToken");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec($curl);
echo $result;
curl_close($curl);
?>
结果:
{
error: {
errors: [
{
domain: "global",
reason: "required",
message: "Login Required",
locationType: "header",
location: "Authorization"
}
],
code: 401,
message: "Login Required"
}
}
我哪里做错了,我该怎么办?从现在开始谢谢你。
你不能给 header 这样的不记名类型令牌。你的代码应该是这样的:
<?php
$curl = curl_init();
$authorization = "Authorization: Bearer your_access_token";
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($curl, CURLOPT_URL, "https://www.googleapis.com/androidpublisher/v2/applications/my.package.name/purchases/products/myProductId/tokens/myPurchaseToken");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec($curl);
echo $result;
curl_close($curl);
?>