首页 > 解决方案 > 无法通过 Google API 授权

问题描述

我正在开发一个带有应用内购买的应用,所以我想知道订阅是否过期。我已经有了我的 RefreshToken 和 AccessToken,但是当我试图根据这个获取订阅信息时,我总是得到同样的错误:{“error”:{“errors”:[
{“domain”:“global” ,
“原因”:“authError”,
“消息”:“无效凭据”,
“locationType”:“标题”,
“位置”:“授权”}],
“代码”:401,
“消息”:“无效凭据” }}

我的代码在这里:

 String urlt = " https://www.googleapis.com/androidpublisher/v3/applications/" + PACKAGE_NAME + "/purchases/subscriptions/" + SUBSCR_ID + "/tokens/" + accessToken;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet post = new HttpGet(urlt);
        post.setHeader("Scope" , "https://www.googleapis.com/auth/androidpublisher");
        post.setHeader("Authorization" , "Bearer" + accessToken);
        try {
            org.apache.http.HttpResponse response = client.execute(post);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer buffer = new StringBuffer();
            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                buffer.append(line);
            }

            JSONObject json = new JSONObject(buffer.toString());
           int x = 0;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

请帮忙 ((

标签: androidgoogle-apiauthorizationgoogle-authenticationin-app-subscription

解决方案


参考RFC 6750,“Bearer”和令牌之间需要有一个空格。

将您的代码更改为:

post.setHeader("Authorization" , "Bearer " + accessToken);

推荐阅读