英文:
Golang gads Package Authentication without File
问题
我正在尝试使用一个我找到的Google AdWords API的Golang包。然而,这个包只提供了用于对包含所有凭据的文件进行身份验证的方法/函数。
我对Golang非常陌生,所以不确定如何创建一个新的函数,使用包含必要信息的字符串变量进行身份验证。
该包可以在以下位置找到:
https://github.com/emiddleton/gads
我进行了一些调查,看看是否能够弄清楚。我找到了一个包含信息的文件结构示例。这是一个示例:
{
"oauth2.Config": {
"ClientID": "4585432543254323-f4qfewtg2qtg5esy24t45h.apps.googleusercontent.com",
"ClientSecret": "fa74ehgyjhtrrjtbrsu56hHjhhrtger",
"Endpoint": {
"AuthURL": "https://accounts.google.com/o/oauth2/auth",
"TokenURL": "https://accounts.google.com/o/oauth2/token"
},
"RedirectURL": "oob",
"Scopes": [
"https://adwords.google.com/api/adwords"
]
},
"oauth2.Token": {
"access_token": "jfdsalkfjdskalfjdaksfdasfdsahrtsrgf",
"token_type": "Bearer",
"refresh_token": "g65wurefej87ruy4fcyfdsafdsafdsafsdaf4fu",
"expiry": "2015-03-05T00:13:23.382907238+09:00"
},
"gads.Auth": {
"CustomerId": "INSERT_YOUR_CLIENT_CUSTOMER_ID_HERE",
"DeveloperToken": "INSERT_YOUR_DEVELOPER_TOKEN_HERE",
"UserAgent": "tests (Golang 1.4 github.com/emiddleton/gads)"
}
}
这是一个JSON对象。我看到该包使用以下函数来获取信息:
func NewCredentialsFromFile(pathToFile string, ctx context.Context) (ac AuthConfig, err error) {
data, err := ioutil.ReadFile(pathToFile)
if err != nil {
return ac, err
}
if err := json.Unmarshal(data, &ac); err != nil {
return ac, err
}
ac.file = pathToFile
ac.tokenSource = ac.OAuth2Config.TokenSource(ctx, ac.OAuth2Token)
ac.Auth.Client = ac.OAuth2Config.Client(ctx, ac.OAuth2Token)
return ac, err
其中pathToFile已经定义。路径将指向将放置在用户主目录中的JSON文件。
如何添加另一个不依赖于使用文件进行身份验证的函数最好呢?
英文:
I'm trying to use a google adwords api golang package I found. However, this package only has methods/functions for authenticating against a file that contains all the credentials.
I am very new to Golang so I'm not sure how to go about creating a new function to authenticate using string variables that contain the necessary information.
The package can be found at:
https://github.com/emiddleton/gads
I did some digging to see if I can figure it out. I found an example for the structure of the file that contains the information. Here's an example:
{
"oauth2.Config": {
"ClientID": "4585432543254323-f4qfewtg2qtg5esy24t45h.apps.googleusercontent.com",
"ClientSecret": "fa74ehgyjhtrrjtbrsu56hHjhhrtger",
"Endpoint": {
"AuthURL": "https://accounts.google.com/o/oauth2/auth",
"TokenURL": "https://accounts.google.com/o/oauth2/token"
},
"RedirectURL": "oob",
"Scopes": [
"https://adwords.google.com/api/adwords"
]
},
"oauth2.Token": {
"access_token": "jfdsalkfjdskalfjdaksfdasfdsahrtsrgf",
"token_type": "Bearer",
"refresh_token": "g65wurefej87ruy4fcyfdsafdsafdsafsdaf4fu",
"expiry": "2015-03-05T00:13:23.382907238+09:00"
},
"gads.Auth": {
"CustomerId": "INSERT_YOUR_CLIENT_CUSTOMER_ID_HERE",
"DeveloperToken": "INSERT_YOUR_DEVELOPER_TOKEN_HERE",
"UserAgent": "tests (Golang 1.4 github.com/emiddleton/gads)"
}
}
It's a JSON object. I see the package is using the following function to bring in the information:
func NewCredentialsFromFile(pathToFile string, ctx context.Context) (ac AuthConfig, err error) {
data, err := ioutil.ReadFile(pathToFile)
if err != nil {
return ac, err
}
if err := json.Unmarshal(data, &ac); err != nil {
return ac, err
}
ac.file = pathToFile
ac.tokenSource = ac.OAuth2Config.TokenSource(ctx, ac.OAuth2Token)
ac.Auth.Client = ac.OAuth2Config.Client(ctx, ac.OAuth2Token)
return ac, err
Where pathToFile is is already defined. The path would be to the json file that would be placed in the user's home directory.
What would be the best way to add another function that does not rely on using a file for the credentials?
答案1
得分: 2
假设您的凭据以 JSON 字符串的形式存在,请按照以下方式在您的代码中创建 AuthConfig
:
func NewCredentialsFromStr(config string, ctx context.Context) (ac gads.AuthConfig, err error) {
if err := json.Unmarshal([]byte(config), &ac); err != nil {
return ac, err
}
ac.file = pathToFile
ac.tokenSource = ac.OAuth2Config.TokenSource(ctx, ac.OAuth2Token)
ac.Auth.Client = ac.OAuth2Config.Client(ctx, ac.OAuth2Token)
return ac, err
}
请注意,这里假设您已经定义了 pathToFile
变量,并且 gads.AuthConfig
类型的字段已经正确设置。
英文:
Assuming you have the credentials in a string as json, make the AuthConfig
in your code like:
func NewCredentialsFromStr(config string, ctx context.Context) (ac gads.AuthConfig, err error) {
if err := json.Unmarshal(config, &ac); err != nil {
return ac, err
}
ac.file = pathToFile
ac.tokenSource = ac.OAuth2Config.TokenSource(ctx, ac.OAuth2Token)
ac.Auth.Client = ac.OAuth2Config.Client(ctx, ac.OAuth2Token)
return ac, err
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论