2013-10-07 59 views
0

我正在開發一款應用程序,其中包含幾種非易耗品inApp產品,我需要通過直接向Google服務器發送請求並接收結果來獲取這些應用程序的購買狀態。正如我所理解的「購買狀態API」提供了這種可能性,但它不適合我。我在「Google API Console」中完成了所有必要的設置: 切換到「Google Play Android開發者API」。 爲我的應用程序創建了一個客戶端ID。 我試過用這裏描述的方法:http://developer.android.com/google/play-services/auth.html 但是方法GoogleAuthUtil.getToken();和GoogleAuthUtil.getTokenWithNotification();正在拋出一個「未知的例外」。 但是我設法通過使用此代碼來獲得訪問令牌:如何使用購買狀態API

AccountManager acountM=AccountManager.get(Utils.curentActivity); 
     Account[] acount=acountM.getAccountsByType("com.google"); 
     String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/androidpublisher"; 
     AccountManagerFuture<Bundle> future=acountM.getAuthToken(acount[0], AUTH_TOKEN_TYPE, null, Utils.curentActivity, null, null); 
     try{ 
      String token=future.getResult().getString(AccountManager.KEY_AUTHTOKEN); 
     }catch(Exception e){e.printStackTrace();} 

但在此之後,我不知道如何處理此令牌。如果我嘗試驗證是這樣的:

URL url=new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token="+token); 
      URLConnection conection=url.openConnection(); 
      conection.setDoInput(true); 
      conection.connect(); 
      InputStream is=conection.getInputStream(); 

它引發了一個「文件未找到異常」。 如果我試圖讓一個產品的購買狀態:

String packageName=MainActivity.this.getPackageName(); 
      String productId="SKU_KEY_1"; 
      HttpClient client=new DefaultHttpClient(); 
      HttpGet get=new HttpGet("https://www.googleapis.com/androidpublisher/v1.1/applications/"+packageName+"/inapp/"+productId+"/purchases/"+token); 
      HttpResponse response=client.execute(get); 
      InputStream is=response.getEntity().getContent(); 
      String result=Utils.readInputStream(is); 
      is.close(); 

結果是JSON它看起來像這樣:

{ 
"error": { 
"errors": [ 
{ 
"domain": "global", 
"reason": "required", 
"message": "Login Required", 
"locationType": "header", 
"location": "Authorization" 
} 
], 
"code": 401, 
"message": "Login Required" 
} 
} 

我拼命地尋找了好幾天的解決方案。如果有人爲我提供解決方案或最新文檔,我會很高興。

+0

檢查http://stackoverflow.com/a/24264696/165708爲解決這個問題。 –

回答

0

因此,查看您的代碼和信息,我會嘗試一些事情,首先驗證您的訪問令牌。我用this作爲參考。使用瀏覽器和一個簡單的html頁面(見下文)我能夠獲得令牌並進行驗證。您需要填寫該頁面上指定的值。

請確保您的令牌是正確的。我通過在瀏覽器中使用該用戶信息地址來做到這一點。

接下來你需要在inapp購買html位置嗎?access_token = {access_token} 如果你注意到上面關於授權的「訪問API」部分,你需要添加這個。

以下是我用來幫助獲取測試令牌的網頁。請注意,這是用於測試的,並且不會用作最終解決方案,因爲授權令牌僅適用於短時間。您需要確保獲取該訪問令牌的代碼正常運行。 另外再次參考上面的頁面填寫您的產品。這裏唯一動態的是來自該頁面上的指令的「代碼」值。

希望一些這可以幫助你......

<form action=" https://accounts.google.com/o/oauth2/token" method="post"> <input name="grant_type" type="hidden" value="authorization_code" /> <input name="code" type="hidden" value="the code from the previous step"/> <input name="client_id" type="hidden" value="the client ID token created in the APIs Console"/> <input name="client_secret" type="hidden" value="the client secret corresponding to the client ID"/> <input name="redirect_uri" type="hidden" value="the URI registered with the client ID"/> <input type="submit" />
</form>

+0

哦,請確保您已在https://code.google.com/apis/console中開啓Google Play Android開發人員API – vbbartlett

相關問題