【发布时间】:2015-11-25 02:37:06
【问题描述】:
我需要在 python 中获取身份验证密钥:auth = requests.auth.HTTPBasicAuth(client_id, client_secret) 代码。如何在 java 中获取它(我使用 OkHttp for Android 应用程序)。
【问题讨论】:
标签: java android python http okhttp
我需要在 python 中获取身份验证密钥:auth = requests.auth.HTTPBasicAuth(client_id, client_secret) 代码。如何在 java 中获取它(我使用 OkHttp for Android 应用程序)。
【问题讨论】:
标签: java android python http okhttp
使用 OkHttp,您可以像这样在您的 OkHttpClient 上创建 Authenticator
OkHttpClient myOkHttpClient = new OkHttpClient();
myOkHttpClient.setAuthenticator(new Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String credential = Credentials.basic("Username", "Password");
return response.request().newBuilder().header("Authorization", credential).build();
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
});
这应该可以完成工作。我发现没有太多关于使用基本身份验证的文档,但我从以下位置找到了一些帮助:Android OkHttp with Basic Authentication
【讨论】: