0
我需要獲取身份驗證密鑰:auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
python代碼。我怎樣才能得到它在Java(我使用OkHttp的Android應用程序)。OkHttp中的HTTPBasicAuth(python)等價物(Android)
我需要獲取身份驗證密鑰:auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
python代碼。我怎樣才能得到它在Java(我使用OkHttp的Android應用程序)。OkHttp中的HTTPBasicAuth(python)等價物(Android)
隨着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
方法身份驗證在服務器返回401錯誤時有效。最好的方法是在攔截器的Header中設置證書。 – qpator