使用golang进行Google Cloud Storage身份验证

huangapple go评论77阅读模式
英文:

using golangGoogle Cloud Storage Authentication

问题

我有一个在服务器上运行的Go应用程序。该应用程序需要访问Google Cloud Storage以保存图像。从Google Cloud Storage身份验证文档中可以看到如何创建PKCS12密钥。

我正在使用:

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

如何在应用程序中使用这个密钥与Go的"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

得分: 2

func ExampleJWTConfigFromJSON() {
// 你的凭证应该从 Google 开发者控制台(https://console.developers.google.com)获取。
// 导航到你的项目,然后查看“凭证”页面
// 在“API 和身份验证”下。
// 要创建一个服务帐号客户端,点击“创建新的客户端 ID”
// 选择“服务帐号”,然后点击“创建客户端 ID”。然后会下载一个 JSON
// 密钥文件到你的计算机。
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)
}
// 初始化一个 http.Client。以下 GET 请求将
// 代表你的服务帐号进行授权和认证。
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
}

详细信息请参考:

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

希望这能帮到你。

英文:
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.

huangapple
  • 本文由 发表于 2015年10月18日 01:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/33189486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定