简体   繁体   中英

How can I hard-code client_id and client_secret of Google OAuth's in my java application?

I am following the Google Drive's API with java ( https://developers.google.com/drive/v2/web/quickstart/java ), and there is this line of code where it reads a .json file in my project directory (I copied it there) that has client_id and client_secret in it.

    // Load client secrets.
    InputStream in = GoogleDrive.class.getResourceAsStream("client_id.json");
    this.clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();

I made a GoogleDrive.java class that makes logging in, uploading etc. easier.

How can I hard code the information in .json file and have google "load" it to clientsecrets, so that I can use this class in other applications as well. I am kind of a newbie in REST so a step by step note would be appreciated.

Thanks you all.

Found the answer, using this method(well, the same method, different signature) does the trick :)

public GoogleAuthorizationCodeFlow.Builder(HttpTransport transport,
                               JsonFactory jsonFactory,
                               String clientId,
                               String clientSecret,
                               Collection<String> scopes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM