我正在开发一款应用程序,其中包含几种非易耗品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"
}
}
我拼命地寻找了好几天的解决方案。如果有人为我提供解决方案或最新文档,我会很高兴。
检查http://stackoverflow.com/a/24264696/165708为解决这个问题。 –