dongshenchi5364 2015-10-17 17:28
浏览 188
已采纳

使用golangGoogle Cloud Storage身份验证

I have a go application running on a server. The application needs access to save image to Google Cloud Storage. From the Google Cloud Storage authentication documentation is can see how to create a PKCS12 key.

I am using

import(
storage "google.golang.org/api/storage/v1"
)

How do you use this key with the golang "storage" client in an application?

Regards

  • 写回答

1条回答 默认 最新

  • dongxun2903 2015-10-18 17:25
    关注
    func ExampleJWTConfigFromJSON() {
        // Your credentials should be obtained from the Google
        // Developer Console (https://console.developers.google.com).
        // Navigate to your project, then see the "Credentials" page
        // under "APIs & Auth".
        // To create a service account client, click "Create new Client ID",
        // select "Service Account", and click "Create Client ID". A JSON
        // key file will then be downloaded to your computer.
        data, err := ioutil.ReadFile("/path/to/your-project-key.json")
        if err != nil {
            log.Fatal(err)
        }
        conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/bigquery")
        if err != nil {
            log.Fatal(err)
        }
        // Initiate an http.Client. The following GET request will be
        // authorized and authenticated on the behalf of
        // your service account.
        client := conf.Client(oauth2.NoContext)
        client.Get("...")
    }
    

    --

    func JWTConfigFromJSON(jsonKey []byte, scope ...string) (*jwt.Config, error)
    {
        var key struct {
            Email      string `json:"client_email"`
            PrivateKey string `json:"private_key"`
        }
        if err := json.Unmarshal(jsonKey, &key); err != nil {
            return nil, err
        }
        return &jwt.Config{
            Email:      key.Email,
            PrivateKey: []byte(key.PrivateKey),
            Scopes:     scope,
            TokenURL:   JWTTokenURL,
        }, nil
    }
    

    For details :

    https://github.com/golang/oauth2/blob/master/google/example_test.go https://github.com/golang/oauth2/blob/master/google/google.go

    Hope this will help.

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

报告相同问题?

悬赏问题

  • ¥15 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"