如何在我的Java应用程序中对Google OAuth的client_id和client_secret进行硬编码?



我正在使用java(https://developers.google.com/drive/v2/web/quickstart/java)遵循Google云端硬盘的API,并且有一行代码,它读取我的项目目录中的.json文件(我将其复制到那里),其中包含client_id和client_secret。

    // 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();

我做了一个GoogleDrive.java类,使登录,上传等更容易。

怎样才能对.json文件中的信息进行硬编码,并让谷歌将其"加载"到clientsecrets,以便我也可以在其他应用程序中使用此类。我是 REST 的新手,所以一步一步的笔记将不胜感激。

谢谢大家。

找到答案,使用此方法(好吧,相同的方法,不同的签名)可以解决问题:)

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

相关内容

最新更新