dongshen3352 2019-09-20 20:32
浏览 96
已采纳

如何在不将凭据存储在本地的情况下建立google oAuth客户端?

I'm trying to establish a google oAuth2 client with golang.org/x/oauth2/google so that I can schedule some email reminders for myself. If I understand the documentation correctly, the rough process goes like this.

  1. Get an oauth2.Config instance
  2. Use the config to get a token from google
  3. Use token to establish a client

The trouble I'm having is that all of the functions for generating config with golang.org/x/oauth2/google seem to require a local credential file from which to read what I assume are Google API Client id and Client secrets.

That's fine for a persistent runtime but this application will be running in a volatile location without a disk. I feel like I must be missing some obvious way to create the config using my api client id and secret that I have stored in the environment. Am I using this package incorrectly? How can I create an instance of oauth2.Config without having a local file?

  • 写回答

1条回答 默认 最新

  • dtp0760 2019-09-20 23:06
    关注

    I can think of a couple of things you can do:

    1) There is a ConfigFromJSON function that gets a JSON document as []byte. You can figure out what that file looks like, and generate it using the environment variables.

    2) It looks like all the package does is to prepare an oauth2 config. You can prepare the config yourself with the credentials you have, bypassing the library completely.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?