2014-06-20 104 views
9

我在最後的日子裏嘗試了很多事情,但現在我的想法:(使用服務帳戶來驗證谷歌InAppPurchase

我想驗證已經在我的Android應用已經取得了inAppPurchase。

1)我在google API控制檯中創建了一個新的服務帳戶。

1A)服務帳戶列出的Unter權限和具有「可以查看」的權限

2)我使用https://github.com/google/google-api-php-client

3)代碼段的最新版本,從我的PHP腳本:

$client = new Google_Client(); 
$client->setApplicationName('myAppName'); 
$client->setClientId('123456789123-vxoasdt8qwe6awerc9ysdfmjysdfysf64werweria8fh.apps.googleusercontent.com'); 
$key = file_get_contents('/shr/data/stor/b516cexx3123asdf3988345d8133e7f86bfas2553-privatekey.p12'); 
$service_account_name = '[email protected]per.gserviceaccount.com'; 

$client->setScopes(array('https://www.googleapis.com/auth/androidpublisher')); 
$cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/androidpublisher'), $key); 
$client->setAssertionCredentials($cred); 

try { 
    $service = new Google_Service_AndroidPublisher($client); 
    $googleApiResult = $service->inapppurchases->get($externalAppId, $externalProductId, $purchaseToken); 
} catch (Exception $e) { 
    var_dump($e->getMessage()); 
} 
從谷歌

4)響應:

GET https://www.googleapis.com/androidpublisher/v1.1/applications/de.test.myapp/inapp/de.test.inapp.google.balance5eur/purchases/[PURCHASETOKEN]: (401)當前用戶沒有足夠的權限執行請求的操作 。

[PURCHASETOKEN]我是從谷歌收到的購買令牌

5)設置$ cred->子= '[email protected]' 到我的郵箱地址帶來了

錯誤刷新OAuth2令牌,消息:'{「error」: 「unauthorized_client」,「error_description」:「未經授權的客戶端或請求中的 範圍。 }'

回答

29

好的,我找到了解決方案!

您必須將您的API與您的應用程序連接起來。您必須轉到您的Google Play發佈頁面(https://play.google.com/apps/publish),並邀請具有服務帳戶電子郵件的用戶在設置 - >用戶帳戶&權限 - >邀請新用戶並授予其「查看財務報告」權限。

這意味着,您通常可能會看到Google Play控制檯(電子郵件地址:[email protected],角色:財務;電子郵件地址:[email protected])中列出的您域中的多個用戶,角色:管理員),您現在將添加另一個用戶並給他一個財務角色(電子郵件:[email protected],角色:財務)。

謝謝,對我有用,祝你好運!

+2

你救了我的一天!非常感謝你!爲什麼這不在Google文檔中記錄? – glutorange

+0

太棒了,謝謝!你剛從頭痛中拯救了我...... – Mike

+0

不幸的是,這並不能解決我的問題。相同的錯誤響應。 「將您的API與您的應用連接起來」是什麼意思?那是添加服務帳戶電子郵件嗎? –

-1

我認爲你必須使用OAuth 2.0 Web客戶端。

+0

這應該是一個評論 – NSNoob

+0

應該是一個評論。 –

相關問題