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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值