Authentication error: Unable to respond to any of these challenges: {} Android - 401 Unauthorized

后端 未结 4 1578
情书的邮戳
情书的邮戳 2021-02-05 18:09

Authentication error: Unable to respond to any of these challenges: {} Android - 401 Unauthorized

I have taken reference from this link Authentication Error when usi

4条回答
  •  孤街浪徒
    2021-02-05 18:20

    I was running into the same "DefaultRequestDirector: Authentication error: Unable to respond to any of these challenges: {}" problem with Drupal services using the loopj android-async-http library (highly recommend it).

    The key to the solution for me was in Jose L Ugia's comment in one of the answers regarding paying special attention to the JSON output from Drupal. I was trying to catch a JSONObject but the real message was in array format "["Wrong username or password."]". Switching to JSONArray caught the error properly and allowed me to handle it. In your case I believe it is because you are not posting the login credentials as drupal services expects it.

    You should remember with Drupal Services you should do system/connect and grab the session, followed by user/login (and the user/password passed in as parameters) and grab the session and then all your subsequent requests should work. This is why I like using the loopj library because it makes all these requests more manageable. Here is a very basic example of connecting to drupal with loopj. All subsequent posts are easily done using the params.

    public class Loopj {
      private static final String TAG = "loopj";
    
      private static AsyncHttpClient client = new AsyncHttpClient();
      private final PersistentCookieStore myCookieStore;
    
    
      public Loopj(Context context) {
        myCookieStore = new PersistentCookieStore(context);
        client.setCookieStore(myCookieStore);
        client.addHeader("Content-type", "application/json");
      }
    
    
      public void systemConnect(String uri) throws JSONException {
        client.post(uri + "/endpoint/system/connect", new JsonHttpResponseHandler() {
          @Override
          public void onSuccess(JSONObject json) {
            Log.i("TAG", "Connect success =" + json.toString());
          }
    
          @Override
          public void onFailure(Throwable e, String response) {
            Log.e("TAG", "Connect failure");
          }
        });
      }
    
      public void userLogin(String uri) throws JSONException {
        RequestParams params = new RequestParams();
        params.put("username", username);
        params.put("password", password);
        client.post(uri + "/endpoint/user/login", params, new JsonHttpResponseHandler() {
          @Override
          public void onSuccess(JSONArray response) {
            Log.i("TAG", "Login success =" + response.toString());
          }
    
          @Override
          public void onFailure(Throwable e, JSONArray json) {
            Log.e("TAG", "Login failure");
          }
        });
      }
    

提交回复
热议问题