json - 无文件的 Golang gads 包身份验证

标签 json go struct google-ads-api

我正在尝试使用我找到的 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 文件。

添加另一个不依赖于使用凭证文件的功能的最佳方法是什么?

最佳答案

假设您在字符串中拥有 json 形式的凭据,请在您的代码中创建 AuthConfig,如下所示:

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
}

关于json - 无文件的 Golang gads 包身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42539006/

相关文章:

c++ - PROGMEM 中的 Arduino 结构数组

json - 如何根据 'type' 为多个结构手动实现 Codable?

c# - asp.net MVC 4 中的 HttpPost

php - 在客户端使用 JSON 获取 fb 访问 token

c++ - 使用结构标识符来表示 POD 类型和 C 结构

c++ - 0xC0000005 : Access violation reading location 0x00000004

python - Python 中字典列表的条件操作

go - 在 Go 中使用 color.RGBA 结构类型创建新颜色时,我收到错误消息,指出整数格式错误

http - google.com 的 golang 反向代理

http - 如何知道给定请求中HTTP客户端使用的代理