gpt4 book ai didi

android - HTTP 1.1 400 错误请求 - Android 客户端 Asana

转载 作者:搜寻专家 更新时间:2023-11-01 08:06:11 25 4
gpt4 key购买 nike

我只是在玩弄 Asana API。因此,每当我发出请求时,我总是会返回 Bad Request 错误。

所以我得到的是:

public class Asana {

private static String apiKey;
private DefaultHttpClient httpClient;
private final String apiUrl = "https://app.asana.com/api/1.0/";

public Asana(String key) {
apiKey = key;
httpClient = new DefaultHttpClient();
}

public void get(String target) {
new APIRequest().execute(target);
}

private class APIRequest extends AsyncTask<String, Void, HttpResponse> {

protected HttpResponse doInBackground(String... params) {
String query = apiUrl + params[0];

Log.d("#query", query);

HttpGet get = new HttpGet(query);
String cred = apiKey + ':';
String encodedKey = Base64.encodeToString(cred.getBytes(), Base64.DEFAULT);

Log.d("#encodedKey", encodedKey);

get.setHeader("Authorization", "Basic " + encodedKey);
get.setHeader("Content-Type", "application/json; charset=UTF-8");

Log.d("#getRequestLine", get.getRequestLine().toString());

/*httpClient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC),
new UsernamePasswordCredentials(apiKey, "")
);*/

HttpResponse resp = null;
try {
resp = httpClient.execute(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}

return resp;
}

protected void onPostExecute(HttpResponse response) {
StatusLine statusLine = response.getStatusLine();
ByteArrayOutputStream out = new ByteArrayOutputStream();

Log.d("#statusLine", statusLine.toString());

try {
response.getEntity().writeTo(out);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Log.d("#out", out.toString());
}
}

}

使用 cURL 或简单的 REST 客户端 (Chrome),我能够得到正确的结果。

我上面的代码有问题吗?

最佳答案

好的,所以我自己解决了这个问题。我认为问题出在身份验证上。

我现在使用的不是手动设置授权 header ,而是:

get.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(apiKey, ""), "US-ASCII", false));

现在它工作正常,我可以获得正确的结果。

如果有人感兴趣,有一个Java library在 github 上获取 Asana API。

关于android - HTTP 1.1 400 错误请求 - Android 客户端 Asana,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14120593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com