Go 使用谷歌云存储认证
我有一个go应用程序在服务器上运行。应用程序需要访问才能将图像保存到谷歌云存储。从文档中可以看到如何创建PKCS12密钥 我正在使用Go 使用谷歌云存储认证,go,google-cloud-storage,Go,Google Cloud Storage,我有一个go应用程序在服务器上运行。应用程序需要访问才能将图像保存到谷歌云存储。从文档中可以看到如何创建PKCS12密钥 我正在使用 import( storage "google.golang.org/api/storage/v1" ) 如何在应用程序中对golang“存储”客户端使用此密钥 问候 func ExampleJWTConfigFromJSON() { // Your credentials should be obtained from the Google /
import(
storage "google.golang.org/api/storage/v1"
)
如何在应用程序中对golang“存储”客户端使用此密钥
问候
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
}