【问题标题】:Google OAuth 2.0 on server side application服务器端应用程序上的 Google OAuth 2.0
【发布时间】:2016-04-15 14:41:50
【问题描述】:

我正在尝试在 cron 作业中从 shell 脚本创建一个运行命令行的 java 应用程序。在这种情况下,我不会让用户在场同意允许在浏览器中访问。该应用程序将查询一个目录,并将该目录中的视频上传到 YouTube。我是 YouTube api 和 Oauth2.0 的新手。在我所做的所有研究中,我看到很多关于如何完成我想做的事情的相互矛盾的信息。

这是我的代码。请让我知道您认为我哪里出错了。提前致谢!

public static Credential authorize(List scopes, String credentialDatastore) throws IOException {

    // Load client secrets.
    //Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/main/resources/client_secrets.json"));
    Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/main/resources/client_secret_file.apps.googleusercontent.com.json"));
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);

    // Checks that the defaults have been replaced (Default = "Enter X here").
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.out.println(
                "Enter Client ID and Secret from https://console.developers.google.com/project/_/apiui/credential "
                        + "into src/main/resources/client_secrets.json");
        System.exit(1);
    }

    // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
    // THIS IS WHERE I NEED TO CHANGE, TO WORK WITH THIS APP!!!!
   // FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
    FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File("." + "/" + CREDENTIALS_DIRECTORY));
    DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore).setAccessType("offline")
            .build();

    // Build the local server and bind it to port 8080
   // LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
    LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

    // Authorize.
    try {
        Credential returnCreds = new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("xxxxxxxxxxxxxxxxxxx"); 
        returnCreds.refreshToken();

        return returnCreds;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // if not authorized, will return a null credential
    return null;
}

【问题讨论】:

  • 我意识到如果事情最终如我所愿,我将不会使用 localReceiver 来生成浏览器。我什至可以在这里做我想做的事情。我总是会使用同一个 YouTube 帐户,因为我只发布我们的视频。

标签: oauth-2.0


【解决方案1】:

这篇帖子看起来很像我要问的很多内容:

Does Google OAuth2.0 support an OAuth-flow for a Resource Owner Password Credential Flow?

这个实现还没有完成,但它确实看起来不错!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多