クラウドインフラ構築記

現在AWSの構築支援に携わっております。今注視しているのは、GKE、BigQuery、Google Dataflowなどサービスを展開しているGoolge Cloud Platformです。

Dropbox java SDKにて認証情報をファイル(jsonフォーマット)への保存


Dropboxへ接続できるようにWEB認証を利用して認証情報をファイルに保存するサンプルになります。認証情報をファイルに保存しておくことでWEB認証しなくてもファイルのアップロードなどが行えるようになります。HTTP POSTでアップロードするとWEBサーバーの設定、スペックによりアップロードできない場合がありますが、HTTPを使用しないで行えばHTTP POSTの制約はなくなります。


    	AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
        WebAuthSession targetSession = new WebAuthSession(appKeys,
        WebAuthInfo authInfo = targetSession.getAuthInfo();

        RequestTokenPair pair = authInfo.requestTokenPair;
        String url = authInfo.url;

        Desktop.getDesktop().browse(new URL(url).toURI());
        JOptionPane.showMessageDialog(null, "Press ok to continue once you have authenticated.");
        String uid = targetSession.retrieveWebAccessToken(pair);

        State state = new State(appKeys);

        state.links.put(uid, targetSession.getAccessTokenPair());
        state.save(STATE_FILE);

    }
  

 

    // ------------------------------------------------------------------------
    private static RuntimeException die(String message)
    {
        System.err.println(message);
        return die();
    }
    private static RuntimeException die()
    {
        System.exit(1);
        return new RuntimeException();
    }
    // ------------------------------------------------------------------------
    // State model (load+save to JSON)
    public static final class State
    {
        public final AppKeyPair appKey;
        public final Map links = new HashMap();
        public State(AppKeyPair appKey)
        {
            this.appKey = appKey;
        }
        public void save(String fileName)
        {
            JSONObject jstate = new JSONObject();
            // Convert app key
            JSONArray japp = new JSONArray();
            japp.add(appKey.key);
            japp.add(appKey.secret);
            jstate.put("app_key", japp);
            // Convert 'Link' objects (uid -> access token)
            JSONObject jlinks = new JSONObject();
            for (Map.Entry link : links.entrySet()) {
                String uid = link.getKey();
                AccessTokenPair access = link.getValue();
                JSONArray jaccess = new JSONArray();
                jaccess.add(access.key);
                jaccess.add(access.secret);
                jlinks.put(uid, jaccess);
            }
            jstate.put("links", jlinks);
            try {
                FileWriter fout = new FileWriter(fileName);
                try {
                    jstate.writeJSONString(fout);
                }
                finally {
                    fout.close();
                }
            }
            catch (IOException ex) {
                throw die("ERROR: unable to save to state file \"" + fileName + "\": " + ex.getMessage());
            }
        }
        public static State load(String fileName)
        {
            JsonThing j;
            try {
                FileReader fin = new FileReader(fileName);
                try {
                    j = new JsonThing(new JSONParser().parse(fin));
                } catch (ParseException ex) {
                    throw die("ERROR: State file \"" + fileName + "\" isn't valid JSON: " + ex.getMessage());
                } finally {
                    fin.close();
                }
            }
            catch (IOException ex) {
                throw die("ERROR: unable to load state file \"" + fileName + "\": " + ex.getMessage());
            }
            try {
                JsonMap jm = j.expectMap();
                JsonList japp = jm.get("app_key").expectList();
                AppKeyPair appKey = new AppKeyPair(japp.get(0).expectString(), japp.get(1).expectString());
                State state = new State(appKey);
                JsonMap jlinks = jm.get("links").expectMap();
                for (Map.Entry jlink : jlinks) {
                    JsonList jaccess = jlink.getValue().expectList();
                    AccessTokenPair access = new AccessTokenPair(jaccess.get(0).expectString(), jaccess.get(1).expectString());
                    state.links.put(jlink.getKey(), access);
                }
                return state;
            }
            catch (JsonExtractionException ex) {
                throw die ("ERROR: State file has incorrect structure: " + ex.getMessage());
            }
        }
    }
    public static final class Link
    {
        public final String uid;
        public final AccessTokenPair accessToken;
        public Link(String uid, AccessTokenPair accessToken)
        {
            this.uid = uid;
            this.accessToken = accessToken;
        }
    }

    private static String getLinkid(String fileName) {
        JsonThing j;
        String linkid = "";

        try {
            FileReader fin = new FileReader(fileName);
            try {
                j = new JsonThing(new JSONParser().parse(fin));
            } catch (ParseException ex) {
                throw die("ERROR: State file \"" + fileName + "\" isn't valid JSON: " + ex.getMessage());
            } finally {
                fin.close();
            }
        }
        catch (IOException ex) {
            throw die("ERROR: unable to load state file \"" + fileName + "\": " + ex.getMessage());
        }
        try {
            JsonMap jm = j.expectMap();
            JsonMap jlinks = jm.get("links").expectMap();

            for (Map.Entry jlink : jlinks) {
                linkid = jlink.getKey();
                break;
            }
            return linkid;
        }
        catch (JsonExtractionException ex) {
            throw die ("ERROR: State file has incorrect structure: " + ex.getMessage());
        }
    }
}

コメントは受け付けていません。