ドロップボックスアカウントのアクセストークンを取得してに渡すサーバープログラムを用意する予定です。
ドロップボックスフォルダにアップグレードするクライアントプログラム。クライアントはDBアカウントやログインを必要とせず、ファイルを私のDBフォルダーに送信できます(したがって、OAuthを使用していません...)。似たようなもの:
ただし、ユーザーが最初にサーバーにアップロードしない場合、つまり、ユーザーがアクセストークンを取得すると、DBに直接アップロードします。
Apache httpclient 4.3を使用して、ブラウザーをシミュレートしてリクエストトークンの取得を実行し、login-infoを送信してアクセストークンを取得しようとしましたが、フォームへの投稿を介してファイルをアップロードするのに行き詰まりました。エラーはHTTP400 BadRequestです..。
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
ログインとファイルのアップロードを行うときにFirefoxLiveHttpHeaderを使用してヘッダーをキャプチャし、ファイルへの投稿のアップロードが実際にこれを行っている(そしてコードに反映している)ことを確認しました。
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:
private String postToUpload(HttpClient client、String accessToken、String localFileInfo、String destPath)throws Exception { String bjarString = "bjar"; 文字列blidString = "blid"; 文字列bjar = null; 文字列blid = null; リストcookies = gcookies.getCookies(); for(Cookie aCookie:cookies){ String 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 = new URIBuilder()。 setScheme( "https")。 setHost( "dl-web.dropbox.com")。 setPath( "/ upload")。 setParameter( "name" 、filename) .setParameter( "dest"、destPath) .setParameter( "cookie_t"、accessToken) .setParameter( "t"、accessToken) .build(); HttpPost httppost = new HttpPost(uri); httppost.setConfig(localConfig); ghttpContext.setCookieStore(gcookies); FileBody bin = new FileBody(new File(localFileInfo)); StringBodyコメント= new StringBody( "ある種のバイナリファイル"、ContentType.DEFAULT_BINARY); HttpEntity reqEntity = MultipartEntityBuilder.create()。 addPart( "bin"、bin) .addPart( "comment"、comment) .build(); httppost.setEntity(reqEntity); //ヘッダーを追加します httppost.setHeader( "Host"、 "www.dropbox.com"); httppost.setHeader( "User-Agent"、USER_AGENT); httppost.setHeader( "Accept"、 "text / html、application / xhtml + xml、application / 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( "Cache-Control"、 "no-cache"); //エンティティ System.out.println( "\ nexecuting request:" + httppost.getRequestLine());を追加します。 //カスタム応答ハンドラーを作成します public String handleResponse(final HttpResponse response) throws ClientProtocolException、IOException { int status = response.getStatusLine()。getStatusCode(); if(status> = 200){// &&ステータス
DropboxAPIを使用する唯一の方法はOAuthです。OAuthアクセストークン(この場合はアカウントで一度認証することで取得)を取得したら、ヘッダーと本文のファイルの内容を使用してHTTPPUT
を実行する必要があります。https://api-content.dropbox.com/1/files_put/auto/<path>
Authorization: Bearer <token>
アクセストークンを持っている人は誰でも、すべてのファイルを削除したり、個人のDVDコレクションをアップロードしたりできることに注意してください。したがって、そのアクセストークンを共有することはお勧めしません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加