dongpangfu6322 2017-03-01 18:12
浏览 46
已采纳

没有文件的Golang gads软件包身份验证

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条回答 默认 最新

  • douhuike3199 2017-03-03 23:18
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧