state.confファイルを使用すると、トークン/ refres_tokenのペアを認証コードを取得せずにプログラムで更新できます。私が使用するコードスニペットは次のとおりです。
private static BoxAPIConnection getBoxAPIConnection(String client_id, String client_secret, String token, String refresh_token, String stateConfPath) {
String state = null;
try {
logger.info("Getting state.conf: " + stateConfPath + "/state.conf");
InputStream fis = new FileInputStream(stateConfPath + "/state.conf");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
state = br.readLine();
}
catch (FileNotFoundException f) {
try {
// create file if it doesn't exist
PrintWriter writer = new PrintWriter(stateConfPath + "/state.conf", "UTF-8");
writer.println("");
writer.close();
}
catch (Exception w) {
logger.fatal("Exception", w);
}
}
catch (IOException e) {
logger.fatal("IOException", e);
}
BoxAPIConnection api = null;
//if (null == state || "".equals(state)) {
if (!token.equals("") && !refresh_token.equals("")) {
api = new BoxAPIConnection(client_id, client_secret, token, refresh_token);
} else {
logger.info("Restoring state..." + state);
api = BoxAPIConnection.restore(client_id, client_secret, state);
if (api.needsRefresh()) { // this is not a reliable call. It can still throw a 401 below
logger.info("api refreshing...");
api.refresh();
}
else {
logger.info("api good...");
}
}
return api;
}
リフレッシュとアクセストークンを要求する認証方法は、最初の認証コードを取得することによって生成され、これは、ログインするユーザーを必要と私はより多くの読書やサンプルを行っていると私はSDKを使用してthings.Whenのカップルを理解しますボックスにアクセスし、アクセスを許可します。これが完了したら、認証コードを使用して、ポストリクエストを行ってトークンを取得します。私はこれをデーモンプロセスとして実行する必要があるので、これを回避しようとしています。私がこのステップを避けることができるかどうか教えてください。 – user3912976