2014-02-17 29 views
1

我正在使用java的Dropbox api。 第一個應用程序是上傳和下載文件在Dropbox帳戶。 我有一個令牌Dropbox的認證,但是當我嘗試在帳戶上傳文件,我得到錯誤的請求錯誤,如:卡在文件上傳在DropBox中使用java下載應用程序

Exception in thread "main" com.dropbox.core.DbxException$BadResponse: unexpected response code: 401 
    at com.dropbox.core.DbxClient$4.handle(DbxClient.java:274) 
    at com.dropbox.core.DbxClient$4.handle(DbxClient.java:270) 
    at com.dropbox.core.DbxRequestUtil.doGet(DbxRequestUtil.java:265) 
    at com.dropbox.core.DbxClient.doGet(DbxClient.java:1912) 
    at com.dropbox.core.DbxClient.getAccountInfo(DbxClient.java:270) 
    at com.prit.net.Main.main(Main.java:50) 

我的代碼如下package com.prit.net;

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.URISyntaxException; 
import java.util.Locale; 
import com.dropbox.core.DbxAppInfo; 
import com.dropbox.core.DbxClient; 
import com.dropbox.core.DbxEntry; 
import com.dropbox.core.DbxException; 
import com.dropbox.core.DbxRequestConfig; 
import com.dropbox.core.DbxWebAuthNoRedirect; 
import com.dropbox.core.DbxWriteMode; 
public class Main { 

    public static void main(String[] args) throws IOException, DbxException, URISyntaxException { 
    // Get your app key and secret from the Dropbox developers website. 
    final String APP_KEY = "mykey"; // change with yours 
    final String APP_SECRET = "mysecret"; // change with yours 

    DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET); 

    DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0",Locale.getDefault().toString()); 
    DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo); 
    // Have the user sign in and authorize your app. 
    String authorizeUrl = webAuth.start(); 
    //Desktop.getDesktop().browse(new URL(authorizeUrl).toURI()); 
    // System.out.println("1. Go to: " + authorizeUrl); 
    // System.out 
    // .println("2. Click \"Allow\" (you might have to log in first)"); 
    // System.out.println("3. Copy the authorization code."); 
    // String code = new BufferedReader(new InputStreamReader(System.in)) 
    // .readLine().trim(); 
    //DbxAuthFinish authFinish = webAuth.finish(code); 
    // System.out.println("Access token is:"); 
    // System.out.println(authFinish.accessToken.toString()); 

    // save the value of myToken to a file for future use 
    String myToken = "myTokensecretkeyxxxxxxxxxxxxxxxxx"; // change with 
                   // yours 
    // DbxClient client = new DbxClient(config, authFinish.accessToken); 
    DbxClient client = new DbxClient(config, myToken); 
    System.out.println(config); 
    System.out.println(myToken); 

    System.out.println("check1"); 
    File inputFile = new File("C:\\Dev\\foo.txt"); 
    System.out.println("check2"); 
    FileInputStream inputStream = new FileInputStream(inputFile); 
    System.out.println("check3"); 
    try { 
     DbxEntry.File uploadedFile = client.uploadFile("/FileApiDemo/fooup2.txt", 
       DbxWriteMode.add(), inputFile.length(), inputStream); 
     System.out.println("Uploaded: " + uploadedFile.toString()); 
    } catch (Exception e) { 

     e.printStackTrace(); 
    } finally { 
     inputStream.close(); 
    } 

    DbxEntry.WithChildren listing = client.getMetadataWithChildren("/FileApiDemo"); 
    System.out.println("Files in the root path:"); 
    for (DbxEntry child : listing.children) { 
     System.out.println(" " + child.name + ": " + child.toString()); 
    } 

    // download file 
     FileOutputStream outputStream = new FileOutputStream("C:\\Dev\\downloadedfile.txt"); 
     try { 
      DbxEntry.File downloadedFile = client.getFile("/FileApiDemo/fooup2.txt", null, outputStream); 
      System.out.println("Metadata: " + downloadedFile.toString()); 
     }  finally { 
      outputStream.close(); 
     } 

    } 

} ` 
+1

401通常是「未經授權」。你應該再次檢查你的憑證。 – Adrian

回答

0

仔細檢查myToken的值。上面提到的評論代碼大概會讓你獲得一個工作訪問令牌,對吧?如果是這樣,請確保您在該流程末尾看到的訪問令牌是您在後續運行中保存和使用的訪問令牌。

0

這是Dropbox的基本上傳示例。該示例正在使用DropBox API v2。

在示例客戶機變量是來自使用DbxClientV2....

try 
     { 
      File file = new File("C:\\... Storage location of file on local system"); 
      //reads file as input for the method to dropbox 
      InputStream fileupload = new FileInputStream(file); 
      //path in dropbox account to store the file 
      // if want to store it in root just put '/' 
      //if want to store file in a folder '/foldername/' 
      client.files().uploadBuilder("/" + file.getName()) 
        .withMode(WriteMode.OVERWRITE)//always overwrites the existing file in the dropbox folder 
        .uploadAndFinish(fileupload); 
      JOptionPane.showMessageDialog(null, "File uploaded to dropbox"); 
     } 
     //exception handled 
     catch (DbxException e) 
     { 
      //error message for uploading file to dropboxcloud 
      JOptionPane.showMessageDialog(null, "Unable to upload file to Cloud \n Error: " + e); 
     } 
     catch (IOException e) 
     { 
      JOptionPane.showMessageDialog(null, "Unable to upload file to cloud \n Error: " + e); 
     } 

這個基本示例不包括文件上傳進度到雲或任何其他檢查。

希望這有幫助

相關問題