使用PHP的Google Play Developer API

时间:2017-11-05 16:16:43

标签: php authorization google-play-developer-api login-required

我想使用PHP与Google Play Developer API保护并验证针对假冒交易的应用内购买。我已经为此审核了Google文档。我首先通过完成此链接https://developers.google.com/android-publisher/authorization中的步骤创建了access_token。然后我尝试执行此链接https://developers.google.com/android-publisher/api-ref/purchases/products/get#auth中描述的操作,但我失败了。我得到的代码和结果如下。

我的代码:

<?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"
}
}

我在哪里做错了,我该怎么办?谢谢你。

1 个答案:

答案 0 :(得分:0)

您不能使用承载类型令牌给出标题。您的代码应该是:

    <?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);
    ?>