我想将inapp产品发布到Google Play商店。为此,我使用下面的链接
https://developers.google.com/android-publisher/api-ref/inappproducts/insert
这是我的代码:
include_once "templates/base.php";
session_start();
require_once 'Google/Client.php';
require_once 'Google/Google_AndroidPublisherService.php';
require_once 'Google/Service/AndroidPublisher.php';
require_once 'Google/Google_AssertionCredentials.php';
$client_id = 'xxxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxx';
$redirect_uri = 'http://localhost:8080';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/androidpublisher");
$AndroidPublisherService = new Google_Service_AndroidPublisher($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
$data = '{
"packageName": "com.rhyme",
"sku": "com.book2",
"status": "true",
"purchaseType": "subscription",
"defaultPrice": {
"priceMicros": "5000",
"currency": "INR"
},
"prices": {
(key): {
"priceMicros": "",
"currency": ""
}
},
"listings": {
(key): {
"title": "testing",
"description": "testing"
}
},
"defaultLanguage": "en-us",
"subscriptionPeriod": "",
"season": {
"start": {
"month": "09",
"day": "11"
},
"end": {
"month": "12",
"day": "23"
}
},
"trialPeriod": ""
}';
$post = file_get_contents('https://www.googleapis.com/androidpublisher/v2/applications/com.rhyme/inappproducts',null,stream_context_create(array(
'http' => array(
'method' => 'POST',
'content' => $data,
),
)));
echo "<pre>";
print_r($post);
if ($post) {
echo $post;
} else {
echo "POST failed";
}
我正在获取访问令牌。但是当我执行链接https://www.googleapis.com/androidpublisher/v2/applications/com.rhyme/inappproducts
时,它会给出一个json错误,如下所示: -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
我不知道我哪里错了。请为我提供建议。
答案 0 :(得分:-1)
由于您已经在使用API客户端库,因此您应该执行类似这样的操作
$AndroidPublisherService->inappproducts->insert('com.rhyme', $data);
比直接使用file_get_contents
。