如何在没有Box的授权页面的情况下获取访问令牌

时间:2014-02-06 20:28:49

标签: box-api

我被授予了文件夹中的访问权限(协作)。我需要的是每天访问该文件夹并从中获取文件。现在,我生成的开发人员令牌在1小时后到期。有没有办法让authorization code没有第一条腿,这需要一个用户界面。这样,每当我获取文件时,我都可以刷新访问权限。

3 个答案:

答案 0 :(得分:10)

您应该能够在不获取授权码的情况下刷新令牌。发送访问令牌后,还会向您发送刷新令牌。

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

您应该将此刷新令牌存储在安全的位置(钥匙串,加密数据存储,类似),并在会话到期时使用它来刷新会话。

当您从Box收到任何API请求的401 Unauthorized响应时,您可以告诉会话已过期,并且您看到一个值为Bearer realm =的WWW-Authenticate标头。

流程应该类似于:

1)登录Box并获取授权码

2)交换ACCESS TOKEN和REFRESH TOKEN对的授权码(这只需要做一次!)

3)存储刷新令牌

4)开始使用API​​发出请求

5)当在API响应中收到带有WWW-Authenticate标头的401 Unauthorized时,向Box发出一个www-form-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 :(得分:1)

找到一个很好的包来回答我的问题。 :) https://github.com/sookasa/box.py

答案 2 :(得分:1)

根据Sikppy Ta的说法

您可以将第一个令牌保存在文件中,并通过此类文件使用刷新机制。

这是示例

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();
}
相关问题