2013-10-07 43 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