2012-10-28 57 views
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(); 
      } 
    } 

以上是我如何登录。而当做另一个网络服务电话时,我得到了未经授权的。解锁方法需要以下头文件。Android Https OAuth 401未经授权的错误

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", "[email protected]"); 
      asyncHttpClient.addHeader("Authorization", "Bearer ".concat(oauthAccessTokenString)); 
      asyncHttpClient.get("host/[email protected]", 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未授权异常。在我的文档中没有引用,以回叫url。我提供的oauth访问令牌很好,但为什么我仍然得到401?这个秘密与第二次电话有什么关系?我被告知是我需要这样设置我的标题。我也被告知,“对于https,客户端需要能够处理证书的验证。有没有人知道如何解决它?

+0

我认为我的X-Device-Id可能是错误的,我在登录方法中得到它,我做错了吗? –

回答

0

这是一个错误的Web服务地址:(有没有必要处理这个秘密没有任何关系,但是我的文档写得很差,在一个地方他们只提到了标题,然后在另一个地方他们说body是必需的所以只要确保你正确地浏览文档

相关问题