如何使用App Engine的URLFetch服务(使用Java)指定用于发出Basic-Auth请求的用户名和密码?
似乎我可以设置HTTP标头:
URL url = new URL("http://www.example.com/comment");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-MyApp-Version", "2.7.3");
Basic-Auth的适当标题是什么?
答案 0 :(得分:30)
这是http:
上的基本身份验证标头授权:基本base64编码(用户名:密码)
例如:
GET /private/index.html HTTP/1.0
Host: myhost.com
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
您需要这样做:
URL url = new URL("http://www.example.com/comment");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization",
"Basic "+codec.encodeBase64String(("username:password").getBytes());
要做到这一点,你需要获得base64编解码器API,如Apache Commons Codec
答案 1 :(得分:14)
对于那些有兴趣在Python中执行此操作的人(就像我一样),代码如下所示:
result = urlfetch.fetch("http://www.example.com/comment",
headers={"Authorization":
"Basic %s" % base64.b64encode("username:pass")})
答案 2 :(得分:6)
在调用openConnection()之前设置一个Authenticator,
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
由于只有一个全局默认验证器,当多个用户在多个线程中执行URLFetch时,这实际上并不能正常工作。如果是这种情况,我会使用Apache HttpClient。
编辑:我错了。 App Engine不允许使用Authenticator。即使允许,我们也会遇到全局验证器实例的多线程问题。即使您无法创建线程,您的请求仍可能在不同的线程中提供。所以我们只需使用此函数手动添加标题import com.google.appengine.repackaged.com.google.common.util.Base64;
/**
* Preemptively set the Authorization header to use Basic Auth.
* @param connection The HTTP connection
* @param username Username
* @param password Password
*/
public static void setBasicAuth(HttpURLConnection connection,
String username, String password) {
StringBuilder buf = new StringBuilder(username);
buf.append(':');
buf.append(password);
byte[] bytes = null;
try {
bytes = buf.toString().getBytes("ISO-8859-1");
} catch (java.io.UnsupportedEncodingException uee) {
assert false;
}
String header = "Basic " + Base64.encode(bytes);
connection.setRequestProperty("Authorization", header);
}
答案 3 :(得分:4)
使用HttpURLConnection
给了我一些问题(由于某种原因,我尝试连接的服务器不接受身份验证凭据),最后我意识到使用GAE的低级URLFetch实际上要容易得多API(com.google.appengine.api.urlfetch
)如下:
URL fetchurl = new URL(url);
String nameAndPassword = credentials.get("name")+":"+credentials.get("password");
String authorizationString = "Basic " + Base64.encode(nameAndPassword.getBytes());
HTTPRequest request = new HTTPRequest(fetchurl);
request.addHeader(new HTTPHeader("Authorization", authorizationString));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
System.out.println(new String(response.getContent()));
这很有用。
答案 4 :(得分:3)
Apache HttpClient for App Engine有一个包装器
请仔细检查http://esxx.blogspot.com/2009/06/using-apaches-httpclient-on-google-app.html
http://peterkenji.blogspot.com/2009/08/using-apache-httpclient-4-with-google.html
答案 5 :(得分:1)
注意第一个答案:setRequestProperty应该获取没有冒号的属性名称(“授权”而不是“授权:”)。