2012-10-28 77 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是必需的所以只要確保你正確地瀏覽文檔

相關問題