【发布时间】:2013-12-08 11:40:12
【问题描述】:
我计划让一个服务器程序获取我的保管箱帐户访问令牌并传递给
客户端程序上传到我的保管箱文件夹。客户端不需要数据库帐户或登录,并且能够将文件发送到我的数据库文件夹(因此不使用 OAuth ...)。类似于:
但没有用户先上传到服务器,即一旦用户获得访问令牌,他们就直接上传到数据库。
我尝试使用 Apache httpclient 4.3 来模拟浏览器来执行获取请求令牌、发送登录信息以获取访问令牌,但在通过 post 将文件上传到表单时卡住了。错误是 HTTP 400 Bad Request ...
executing request:GET https://www.dropbox.com/login HTTP/1.1
----------------------------------------
HTTP/1.1 200 OK
Request Token: moiejtzdLqTA_0sh3gQyNZAI
executing request:POST https://www.dropbox.com/login HTTP/1.1
----------------------------------------
HTTP/1.1 200 OK
Access Token: 5Ot52QKDbDPSsL1ApU4MIapJ
executing request:POST https://dl-web.dropbox.com/upload?
name=sample.jpg&dest=upload&cookie_t=5Ot52QKDbDP....SsJ&t=5Ot5...apJ HTTP/1.1
----------------------------------------
HTTP/1.1 400 Bad Request
我在登录和上传文件时使用 Firefox LiveHttpHeader 来捕获标头,并看到文件上传实际上是这样做的(并反映在代码中):
https://dl-web.dropbox.com/chunked_upload?
name=tmp1.jpg
&chunk=0
&chunks=1
&bjar=W3sic2Vzc1..............Q%253D%253D
&blid=AAAw4tn................2cDxA
&cookie_t=32yq........nw6c34o
&dest=
&t=32yqVof........c34o
&reported_total_size=5611
&upload_id=1BKGRRP5TpCEjcWSu5tmpQ
&offset=0
所以显然我错过了一些参数,但不知道是什么。访问令牌似乎是有效的,因为我可以在从 httpclinet 帖子到https://www.dropbox.com/home 的返回中看到我的帐户信息,但上传根本不起作用。任何人都有类似的经验并收到 HTTP 400 错误? .... 非常感谢!
部分代码如下:
构造函数和main():
// constructor ...
public HttpClientExample() {
gcookies = new BasicCookieStore();
globalConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.BEST_MATCH)
.build();
// Create local HTTP context
ghttpContext = HttpClientContext.create();
ghttpContext.setCookieStore(gcookies);
//
redirectStrategy = new LaxRedirectStrategy(); // for http redirect ...
httpclient = HttpClients.custom()
.setDefaultRequestConfig(this.globalConfig)
.setDefaultCookieStore(this.gcookies)
.setRedirectStrategy(redirectStrategy)
.build();
} // constructor ...
public static void main(String[] args) throws Exception {
HttpClientExample myhttp = new HttpClientExample();
try {
this.localConfig = RequestConfig.copy(this.globalConfig)
.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY)
.build();
String requestToken = this.getRequestToken(httpclient, loginurl);
theAccessToken = this.postForAccessToken(requestToken, loginurl);
String localFileTopassIn = this.localPath ;
this.postToUpload(httpclient, this.theAccessToken, localFileTopassIn , this.dropboxFolderOnlyName);
}
}
获取请求令牌:
private String getRequestToken(HttpClient client, String theURL) throws Exception {
HttpGet httpget = new HttpGet(theURL);
httpget.setConfig(localConfig);
httpget.setHeader("Connection", "keep-alive");
System.out.println("\nexecuting request:" + httpget.getRequestLine());
// Create a custom response handler
ResponseHandler responseHandler = new ResponseHandler() {
public String handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 ) { // && status cookies = gcookies.getCookies();
for (Cookie aCookie: cookies) {
String cookieName = aCookie.getName();
if ( !(cookieName.lastIndexOf(gvcString) == -1) ) {
gvc = aCookie.getValue();
} else if ( !(cookieName.lastIndexOf(tString) == -1) ) {
requestToken = aCookie.getValue();
}
}
System.out.println("Request Token: " + requestToken );
return requestToken;
}
postForAccessToken:
private String postForAccessToken(HttpClient client, String requestToken, String theURL) throws Exception{
/*
* Send a post together with request token and my login to get accessToken ...
*/
HttpPost httppost = new HttpPost(theURL); // loginurl);
httppost.setConfig(localConfig);
ghttpContext.setCookieStore(gcookies);
List params = new LinkedList();
params.add(new BasicNameValuePair("login_email", myemail));
params.add(new BasicNameValuePair("login_password", mypasswd));
params.add(new BasicNameValuePair("t", requestToken));
HttpEntity postentity = new UrlEncodedFormEntity(params);
httppost.setEntity(postentity);
System.out.println("\nexecuting request:" + httppost.getRequestLine());
// Create a custom response handler
ResponseHandler responseHandler = new ResponseHandler() {
public String handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 ) { // && status cookies = gcookies.getCookies();
for (Cookie aCookie: cookies) {
String cookieName = aCookie.getName();
if ( !(cookieName.lastIndexOf(tString) == -1) ) {
theAccessToken = aCookie.getValue();
}
}
System.out.println("Access Token: " + theAccessToken );
return theAccessToken;
}
postToUpload:
私有字符串 postToUpload(HttpClient 客户端,字符串 accessToken,字符串 localFileInfo,字符串 destPath)抛出异常{ 字符串 bjarString = "bjar"; 字符串 blidString = "blid"; 字符串 bjar=null; 字符串盲=空; 列出 cookie = gcookies.getCookies(); for (Cookie aCookie: cookies) { 字符串 cookieName = aCookie.getName(); if ( !(cookieName.lastIndexOf(bjarString) == -1) ) { bjar = aCookie.getValue(); } else if ( !(cookieName.lastIndexOf(blidString) == -1) ) { blid = aCookie.getValue(); } } String[] fileNameArry = localFileInfo.split("(\\\\|/)"); 字符串文件名 = fileNameArry[fileNameArry.length - 1]; // 获取最后一部分 ... URI uri = 新的 URIBuilder() .setScheme("https") .setHost("dl-web.dropbox.com") .setPath("/上传") .setParameter("名称", 文件名) .setParameter("dest", destPath) .setParameter("cookie_t", accessToken) .setParameter("t", accessToken) 。建造(); HttpPost httppost = 新的 HttpPost(uri); httppost.setConfig(localConfig); ghttpContext.setCookieStore(gcookies); FileBody bin = new FileBody(new File(localFileInfo)); StringBody comment = new StringBody("某种二进制文件", ContentType.DEFAULT_BINARY); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("bin", bin) .addPart(“评论”,评论) 。建造(); httppost.setEntity(reqEntity); // 添加标题 httppost.setHeader("主机", "www.dropbox.com"); httppost.setHeader("用户代理", USER_AGENT); httppost.setHeader("接受", "文本/html,应用程序/xhtml+xml,应用程序/xml;q=0.9,*/*;q=0.8"); httppost.setHeader("连接", "保持活动"); httppost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httppost.setHeader("Pragma", "no-cache"); httppost.setHeader("缓存控制", "无缓存"); // 添加实体 System.out.println("\n执行请求:" + httppost.getRequestLine()); // 创建自定义响应处理程序 ResponseHandler responseHandler = new ResponseHandler() { 公共字符串句柄响应(最终的 HttpResponse 响应) 抛出 ClientProtocolException,IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 ) { // && 状态【问题讨论】:
标签: file-upload dropbox access-token http-status-code-400