douzhan4522 2018-01-06 05:52
浏览 83
已采纳

如何使用GCE Go客户端oauth2身份验证实现自动身份验证

This code is on the basis of golang.org/x/oauth2 example test. I am trying to get instance information from Google Compute Engine using Go client. Do I have to use oauth2 authentication? There is a generated link after Visit the URL for the auth dialog:

https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxx&redirect_uri=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute&state=state

and it redirect to https://www.googleapis.com/auth/compute which shows a 'compute'. How do I achieve automatic authentication?

package main

import (
    "context"
    "fmt"
    "log"

    "golang.org/x/oauth2"
    "google.golang.org/api/compute/v1"
)

type GCE struct {
    *compute.Service
}

var ctx = context.Background()

func initGCE() *GCE {
    conf := &oauth2.Config{
        ClientID:     "xxx",
        ClientSecret: "xxx",
        Scopes:       []string{compute.ComputeScope},
        Endpoint: oauth2.Endpoint{
            AuthURL:  "https://accounts.google.com/o/oauth2/auth",
            TokenURL: "https://accounts.google.com/o/oauth2/auth",
        },
        RedirectURL: "https://www.googleapis.com/auth/compute",

    }
    url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
    fmt.Printf("Visit the URL for the auth dialog: %v", url)
    var code string
    if _, err := fmt.Scan(&code); err != nil {
        log.Fatal(err)
    }
    tok, err := conf.Exchange(ctx, code)
    if err != nil {
        log.Fatal(err)
    }
    service, err := compute.New(conf.Client(ctx, tok))
    if err != nil {
        log.Fatal(err)
    }
    return &GCE{service}
}

func (g *GCE) Instance() {
    project := "arctic-cyclist-189707"
    zone := "us-east1-b"
    instance := "centos7"
    resp, err := g.Instances.Get(project, zone, instance).Context(ctx).Do()
    if err != nil {
        fmt.Println(err)
        return
    } else {
        fmt.Printf("%#v
", resp)

    }
}
  • 写回答

1条回答 默认 最新

  • dsxmwin86342 2018-01-06 15:04
    关注

    Solved by using code example

    https://cloud.google.com/compute/docs/reference/latest/instances/get#examples

    just set "GOOGLE_APPLICATION_CREDENTIALS" environment varibles as google.DefaultClient() requires.
    package main

    import (
        "context"
        "fmt"
        "log"
    
        "golang.org/x/oauth2/google"
        "google.golang.org/api/compute/v1"
    )
    
    type GCE struct {
        *compute.Service
    }
    
    var ctx = context.Background()
    
    func initGCE() *GCE {
        c, err := google.DefaultClient(ctx, compute.CloudPlatformScope)
        if err != nil {
            log.Fatal(err)
        }
    
        computeService, err := compute.New(c)
        if err != nil {
            log.Fatal(err)
        }
        return &GCE{computeService}
    }
    
    func (g *GCE) Instance(project, zone, instance string) {
        resp, err := g.Instances.Get(project, zone, instance).Context(ctx).Do()
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%#v
    ", resp)
    }
    

    Thanks for your reply.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测