Android Https OAuth 401 Unauthorized error

时间:2012-10-28 00:54:04

标签: java android https oauth-2.0

private void login() {
            androidID = Secure.getString(MainActivity.this.getContentResolver(), Secure.ANDROID_ID);
            String uP = androidID.concat(":ClientTrustedSecret");
            byte[] authByteAry = null;
            try {
                    authByteAry = uP.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
            }
            String base64 = Base64.encodeToString(authByteAry, Base64.DEFAULT).trim();
            client.addHeader("Authorization", "Basic ".concat(base64));
            // Following format is required to post to OAuth
            JSONObject jsonObject = new JSONObject();
            try {
                    jsonObject.put("grant_type", "password");
                    jsonObject.put("username", "abc");
                    jsonObject.put("password", "abc");
            } catch (JSONException e1) {
                    e1.printStackTrace();
            }
            String contentType = "application/json; charset=UTF-8";
            StringEntity data = null;
            try {
                    // Send the json to the server
                    data = new StringEntity(jsonObject.toString());
                    client.post(MainActivity.this, baseURL.concat("/tokens"), data, contentType, new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(String response) {
                                    try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            oauthAccessTokenString = jsonObject.get("access_token").toString();
                                    } catch (JSONException e) {
                                            e.printStackTrace();
                                    }
                            }
                            @Override
                            public void onFailure(Throwable t, String err) {
                                    System.out.println("login failed");
                            }
                    });
            } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
            }
    }

以上是我登录的方式。在进行另一次网络电话通话时,我得到了未经授权的通知。解锁方法需要以下标题。

http://i.imgur.com/EaWDO.png

private void unlock()
    {
            AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
            asyncHttpClient.addHeader("Locale", "en_US");
            asyncHttpClient.addHeader("X-Originator-Type", "app");
            asyncHttpClient.addHeader("Content-Type", "application/json");
//              asyncHttpClient.addHeader("Connection", "Keep-Alive");
//              asyncHttpClient.addHeader("X-Device-Id", androidID);
//              asyncHttpClient.addHeader("X-via", deviceId);
//              asyncHttpClient.addHeader("ClientID", "abc@abc.com");
            asyncHttpClient.addHeader("Authorization", "Bearer ".concat(oauthAccessTokenString));
            asyncHttpClient.get("host/users?loginName=abc@abc.com", new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    System.out.println(response);
                }
                @Override
                    public void onFailure(Throwable t, String err) {
                            System.out.println("Unlock server call failed");
                            Log.d("printing error: ", err);
                    }
            });
    }

以上代码抛出401未经授权的异常。我所拥有的文档中没有引用来回调网址。我提供oauth访问令牌就好了,但那为什么我仍然得到401?秘密与第二次通话有关吗?我被告知是我需要以这种方式设置我的标题。我还被告知“对于https,客户端需要能够处理证书的验证。有谁知道如何解决它?

1 个答案:

答案 0 :(得分:0)

这是一个错误的网络服务地址:(没有必要处理证书的验证。没有关于回调网址。秘密无关。但是我的文档编写得非常糟糕,在一个地方,他们只提到标题,然后在另一个地方,他们说身体是必要的。所以只要确保你正确浏览文件。