2014-02-06 59 views
6

我已被授予對文件夾的訪問權限(協作)。我需要的是每天訪問該文件夾並從中獲取文件。現在我生成的開發人員令牌在1小時內過期。有沒有一種方法可以在沒有第一回合的情況下獲得authorization code,這需要用戶界面。這樣我可以刷新訪問toke每當我獲取文件。如何在沒有Box的授權頁面的情況下獲取訪問令牌

回答

10

您應該能夠刷新令牌沒有得到授權碼。當訪問令牌被髮回時,刷新令牌也會發給您。

{ 
    "access_token": "T9cE5asGnuyYCCqIZFoWjFHvNbvVqHjl", 
    "expires_in": 3600, 
    "restricted_to": [], 
    "token_type": "bearer", 
    "refresh_token": "J7rxTiWOHMoSC1isKZKBZWizoRXjkQzig5C6jFgCVJ9bUnsUfGMinKBDLZWP9BgR" 
} 

,可以儲存此刷新令牌安全的地方(鑰匙扣,加密的數據存儲,類似的東西),並用它到期時刷新會話。

當您從箱體爲任何API請求401未授權迴應,你會看到一個WWW-Authenticate頭與承載領域的價值可以說是過期的會話=。

的流動應該是這個樣子:

1)登錄到盒,並得到一個授權碼

2)交換訪問令牌和刷新令牌對授權碼(這隻需要做一次!)

3)存儲刷新令牌

4)開始與API

5發出請求)當一個401未授權與API響應中的WWW-Authenticate頭,問題接受了WWW的形式,進行了urlencoded POST請求到盒子這樣的:

curl https://www.box.com/api/oauth2/token \ -d 'grant_type=refresh_token&refresh_token={valid refresh token}&client_id={your_client_id}&client_secret={your_client_secret}' \ -X POST 

如果成功的話,你會發出一個新的訪問令牌刷新令牌對。存儲新的刷新令牌,換出舊的訪問令牌並恢復先前失敗的調用中的API調用。

希望有幫助!

+1

我不相信這回答了有機磷農藥的問題。你在這裏做了一個假設,他有一個他沒有的刷新令牌,只是開發者令牌,客戶端ID和祕密。似乎沒有辦法通過進行這些oauth2調用刷新開發人員令牌。 – alanwill

1

根據Sikppy鉭

您可以保存您的第一個令牌在文件中,並使用通過此類文件的刷新機制。

這裏是例子

static String tokenUrl = "https://app.box.com/api/oauth2/token"; 

public String getTokenFromFile() throws Exception { 

    String path = this.tokenFilePath; 
    File file = new File(path); 
    String line = "", token = ""; 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     while ((line = br.readLine()) != null) { 
      token = line; 
     } 
     br.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    String newRefleshToken = refleshToken(token); 
    String accessToken = newRefleshToken.substring(17, 49); 
    return accessToken; 
} 

對於refreshToken,你需要HttpClient的

private String refleshToken(String tokencode) throws Exception { 
    String accessToken = tokencode.substring(17, 49); 
    String refleshToken = tokencode.substring(105, 169); 
    tokencode = HttpURLConnectionExample.refreshToken(refleshToken); 
     writeTokenToTextFile(tokencode); 
     return tokencode; 
    } 

public static String refreshToken(String newToken) throws Exception { 


    String urlParameters = "grant_type=refresh_token&refresh_token=" + newToken + "&client_id=" + client_id + "&client_secret=" + client_secret; 

    String result = sendPost(tokenUrl, urlParameters); 
    return result; 
} 

讓我展現sendPost方法

String sendPost(String url, String urlParameters) throws Exception { 

    URL obj = new URL(url); 
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 

    //add reuqest header 
    con.setRequestMethod("POST"); 
    con.setRequestProperty("User-Agent", USER_AGENT); 
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 

    // Send post request 
    con.setDoOutput(true); 
    DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
    wr.writeBytes(urlParameters); 
    wr.flush(); 
    wr.close(); 

    int responseCode = con.getResponseCode(); 
    System.out.println("Response Code : " + responseCode); 

    BufferedReader in = new BufferedReader(
      new InputStreamReader(con.getInputStream())); 
    String inputLine; 
    StringBuffer response = new StringBuffer(); 

    while ((inputLine = in.readLine()) != null) { 
     response.append(inputLine); 
    } 
    in.close(); 

    return response.toString(); 
} 
相關問題