简体   繁体   English

Volley 使用基本 HTTP 身份验证抛出 AuthFailureError (401)

[英]Volley throwing AuthFailureError (401) with Basic HTTP Authentication

I'm trying to make a GET request in an Android application using the Volley library.我正在尝试使用 Volley 库在 Android 应用程序中发出 GET 请求。 This GET request is to verify account credentials using Basic HTTP Authentication.此 GET 请求用于使用基本 HTTP 身份验证验证帐户凭据。 I verified the URL with credentials works in my browser as it returns successful XML.我验证了带有凭据的 URL 在我的浏览器中有效,因为它返回了成功的 XML。 The format is:格式为:

http://username:password@myanimelist.net/api/account/verify_credentials.xml

where username and password obviously represent real user credentials.其中usernamepassword显然代表真实的用户凭据。 Volley throws this error: Volley 抛出此错误:

 BasicNetwork.performRequest: Unexpected response code 401 for http://username:password@myanimelist.net/api/account/verify_credentials.xml

Here is my Android code that handles the request:这是我处理请求的Android代码:

private static final String HTTP_PRE = "http://";
private static final String VERIFY_CREDENTIALS = "myanimelist.net/api/account/verify_credentials.xml";

public void verifyCredentials(String username, String password) {
    RequestQueue queue = Volley.newRequestQueue(context);
    String url = HTTP_PRE + username + ":" + password + "@" + VERIFY_CREDENTIALS;

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            processResponse(response);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            // handle error
            Log.d(TAG, "error: " + error.getMessage());
        }
    });

    queue.add(stringRequest);
}

This solution to override the getHeaders() method provided the same result: How does one use Basic Authentication with Volley on Android?这种覆盖 getHeaders() 方法的解决方案提供了相同的结果: How do you use Basic Authentication with Volley on Android? . .

Here is my implementation of that solution:这是我对该解决方案的实现:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> params = super.getHeaders();
    if (params == null){
        params = new HashMap<>();
    }
    String creds = String.format("%s:%s", username, password);

    params.put("Authorization", creds);

    return params;
}

Which returned this error without the credentials built directly into the URL:在没有直接内置到 URL 中的凭据的情况下返回此错误:

BasicNetwork.performRequest: Unexpected response code 401 for http://myanimelist.net/api/account/verify_credentials.xml

If someone could provide advice, I'd really appreciate it.如果有人可以提供建议,我将不胜感激。 This is my first time using Basic HTTP Authentication so I could be missing something obvious.这是我第一次使用基本 HTTP 身份验证,所以我可能会遗漏一些明显的东西。

I solved this problem following the first answer here: Http Authentication in android using volley library .我按照这里的第一个答案解决了这个问题: Http Authentication in android using volley library I had tried something similar and many other solutions but this was the only one that worked.我曾尝试过类似的解决方案和许多其他解决方案,但这是唯一有效的解决方案。

Basic Authentication uses BASE64 encoding.基本身份验证使用 BASE64 编码。 You're missing你不见了

String creds = String.format("%s:%s", username, password);
creds = Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);

The Authorization HTTP header requires to indicate the method used (Basic|Digest). Authorization HTTP 标头需要指明使用的方法(基本|摘要)。 At last you headers should look like this:最后你的标题应该是这样的:

GET http://username:password@myanimelist.net/api/account/verify_credentials.xml
Accept: text/xml,text/plain
...
Authorization: Basic XXXXXXXXXXXXXX==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM