我已被授予對文件夾的訪問權限(協作)。我需要的是每天訪問該文件夾並從中獲取文件。現在我生成的開發人員令牌在1小時內過期。有沒有一種方法可以在沒有第一回合的情況下獲得authorization code
,這需要用戶界面。這樣我可以刷新訪問toke每當我獲取文件。如何在沒有Box的授權頁面的情況下獲取訪問令牌
6
A
回答
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
找到了一個很好的軟件包來回答我的問題。 :) https://github.com/sookasa/box.py
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();
}
相關問題
- 1. 在沒有瀏覽器的情況下獲取訪問令牌
- 2. 獲取Facebook頁面訪問令牌沒有頁面所有權
- 3. 在沒有使用Facebook SDK的情況下獲取Facebook授權令牌
- 4. 如何在沒有任何登錄的情況下獲取訪問令牌?
- 5. 如何在沒有訪問令牌的情況下獲取Google通訊錄?
- 6. Rest fb - 授權url獲取頁面訪問令牌
- 7. 獲取訪問令牌和授權
- 8. 從授權獲取Facebook令牌訪問
- 9. 如何在沒有頁面訪問令牌的情況下從Facebook公共頁面提取評論?
- 10. 允許在沒有授權的情況下訪問單個API?
- 11. 如何獲取頁面訪問令牌
- 12. 在沒有用戶交互的情況下獲取Office 365 API訪問令牌
- 13. 在沒有實際訪問頁面的情況下獲取網頁源代碼
- 14. 如何在沒有頁面的情況下獲取網址
- 15. Facebook授權訪問令牌?
- 16. 如何在沒有CentOS設置帳戶的情況下授予訪問權限
- 17. 如何在沒有SuperSU的情況下授予root訪問權限
- 18. 如何在不回撥URL的情況下獲取訪問令牌和訪問令牌密鑰Onedrive
- 19. 如何使用授權碼獲取訪問令牌?
- 20. 如何在沒有sudo權限的情況下訪問硬盤?
- 21. 如何在沒有應用的情況下擴展Facebook訪問令牌?
- 22. 未經授權訪問訪問令牌
- 23. 獲取授權令牌
- 24. 如何在不迭代帳戶列表的情況下獲取Facebook頁面訪問令牌?
- 25. Instagram API在沒有授權的情況下拉取圖片
- 26. 無法獲取頁面訪問令牌
- 27. 從FQL獲取頁面訪問令牌?
- 28. iOS Instagram - 如何在沒有登錄的情況下獲取公共提要的訪問令牌
- 29. PHP - 如何在沒有API訪問令牌的情況下從Instagram獲取圖片
- 30. 如何在沒有瀏覽器重定向的情況下爲Google API獲取訪問令牌
我不相信這回答了有機磷農藥的問題。你在這裏做了一個假設,他有一個他沒有的刷新令牌,只是開發者令牌,客戶端ID和祕密。似乎沒有辦法通過進行這些oauth2調用刷新開發人員令牌。 – alanwill