当前位置: 首页>>代码示例>>Golang>>正文


Golang jwt.NewToken函数代码示例

本文整理汇总了Golang中code/google/com/p/goauth2/oauth/jwt.NewToken函数的典型用法代码示例。如果您正苦于以下问题:Golang NewToken函数的具体用法?Golang NewToken怎么用?Golang NewToken使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewToken函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewDriverGCE

func NewDriverGCE(ui packer.Ui, projectId string, c *clientSecrets, key []byte) (Driver, error) {
	log.Printf("[INFO] Requesting token...")
	log.Printf("[INFO]   -- Email: %s", c.Web.ClientEmail)
	log.Printf("[INFO]   -- Scopes: %s", DriverScopes)
	log.Printf("[INFO]   -- Private Key Length: %d", len(key))
	log.Printf("[INFO]   -- Token URL: %s", c.Web.TokenURI)
	jwtTok := jwt.NewToken(c.Web.ClientEmail, DriverScopes, key)
	jwtTok.ClaimSet.Aud = c.Web.TokenURI
	token, err := jwtTok.Assert(new(http.Client))
	if err != nil {
		return nil, err
	}

	transport := &oauth.Transport{
		Config: &oauth.Config{
			ClientId: c.Web.ClientId,
			Scope:    DriverScopes,
			TokenURL: c.Web.TokenURI,
			AuthURL:  c.Web.AuthURI,
		},
		Token: token,
	}

	log.Printf("[INFO] Instantiating client...")
	service, err := compute.New(transport.Client())
	if err != nil {
		return nil, err
	}

	return &driverGCE{
		projectId: projectId,
		service:   service,
		ui:        ui,
	}, nil
}
开发者ID:Nitron,项目名称:packer,代码行数:35,代码来源:driver_gce.go

示例2: initGoogleService

func (uploader *Uploader) initGoogleService() error {
	token := jwt.NewToken(
		uploader.config.ClientEmail,
		strings.Join(GOOGLE_SERVICE_SCOPE, " "),
		[]byte(uploader.config.PrivateKey),
	)

	client := &http.Client{}

	oauthToken, err := token.Assert(client)
	if err != nil {
		return err
	}

	transport := &oauth.Transport{
		Token: oauthToken,
	}

	c := transport.Client()

	_, err = oauth2.New(c)
	if err != nil {
		return err
	}

	driveService, err := drive.New(c)
	if err != nil {
		return err
	}

	uploader.driveService = driveService

	return nil
}
开发者ID:Konboi,项目名称:tanaage,代码行数:34,代码来源:uploader.go

示例3: getOauthClient

func (s *Gcs) getOauthClient() *http.Client {
	// Read the secret file bytes into the config.
	secret_bytes, err := ioutil.ReadFile(s.SecretFile)
	if err != nil {
		log.Fatal("error reading secrets file: ", err)
		return nil
	}

	var config struct {
		Web struct {
			ClientEmail string `json:"client_email"`
			ClientID    string `json:"client_id"`
			TokenURI    string `json:"token_uri"`
		}
	}
	err = json.Unmarshal(secret_bytes, &config)
	if err != nil {
		log.Println(s.SecretFile)
		log.Fatal("error unmarshalling secrets: ", err)
		return nil
	}

	// Get the project ID from the client ID.
	project_id := strings.SplitN(config.Web.ClientID, "-", 2)[0]
	log.Println("project_id: ", project_id)

	// Read the pem file bytes for the private key.
	key_bytes, err := ioutil.ReadFile(s.PemFile)
	if err != nil {
		log.Fatal("error reading private key file: ", err)
		return nil
	}

	// Craft the ClaimSet and JWT token.
	scope := gcs.DevstorageFull_controlScope
	token := jwt.NewToken(config.Web.ClientEmail, scope, key_bytes)
	token.ClaimSet.Aud = config.Web.TokenURI

	// We need to provide a client.
	client := &http.Client{}

	// Get the access token.
	oauth_token, err := token.Assert(client)
	if err != nil {
		log.Fatal("assertion error: ", err)
		return nil
	}

	// Refresh token will be missing, but this access_token will be good
	// for one hour.
	log.Printf("access_token = %v\n", oauth_token.AccessToken)
	log.Printf("refresh_token = %v\n", oauth_token.RefreshToken)
	log.Printf("expires %v\n", oauth_token.Expiry)

	transport := &oauth.Transport{Token: oauth_token}

	return transport.Client()
}
开发者ID:peteraba,项目名称:go-blah,代码行数:58,代码来源:gcs.go

示例4: NewShortener

func NewShortener(issuer string, key []byte) (s *Shortener, err error) {
	s = &Shortener{}
	token := jwt.NewToken(issuer, urlshortener.UrlshortenerScope, key)
	if s.transport, err = jwt.NewTransport(token); err != nil {
		return
	}
	s.svc, err = urlshortener.New(s.transport.Client())
	return
}
开发者ID:smscloud,项目名称:smscloud,代码行数:9,代码来源:googl.go

示例5: GetServiceAccountToken

func GetServiceAccountToken(config *ServiceAccountConfig) (*oauth.Token, error) {
	token := jwt.NewToken(config.ClientEmail, strings.Join(config.Scope, " "), []byte(config.PrivateKey))

	client := &http.Client{}
	oauthToken, err := token.Assert(client)
	if err != nil {
		return nil, err
	}

	return oauthToken, nil
}
开发者ID:kayac,项目名称:alphawing,代码行数:11,代码来源:googleservice.go

示例6: loadAndValidate

func (c *Config) loadAndValidate() error {
	var account accountFile
	var secrets clientSecretsFile

	if err := loadJSON(&account, c.AccountFile); err != nil {
		return fmt.Errorf(
			"Error loading account file '%s': %s",
			c.AccountFile,
			err)
	}

	if err := loadJSON(&secrets, c.ClientSecretsFile); err != nil {
		return fmt.Errorf(
			"Error loading client secrets file '%s': %s",
			c.ClientSecretsFile,
			err)
	}

	// Get the token for use in our requests
	log.Printf("[INFO] Requesting Google token...")
	log.Printf("[INFO]   -- Email: %s", account.ClientEmail)
	log.Printf("[INFO]   -- Scopes: %s", clientScopes)
	log.Printf("[INFO]   -- Private Key Length: %d", len(account.PrivateKey))
	log.Printf("[INFO]   -- Token URL: %s", secrets.Web.TokenURI)
	jwtTok := jwt.NewToken(
		account.ClientEmail,
		clientScopes,
		[]byte(account.PrivateKey))
	jwtTok.ClaimSet.Aud = secrets.Web.TokenURI
	token, err := jwtTok.Assert(new(http.Client))
	if err != nil {
		return fmt.Errorf("Error retrieving auth token: %s", err)
	}

	// Instantiate the transport to communicate to Google
	transport := &oauth.Transport{
		Config: &oauth.Config{
			ClientId: account.ClientId,
			Scope:    clientScopes,
			TokenURL: secrets.Web.TokenURI,
			AuthURL:  secrets.Web.AuthURI,
		},
		Token: token,
	}

	log.Printf("[INFO] Instantiating GCE client...")
	c.clientCompute, err = compute.New(transport.Client())
	if err != nil {
		return err
	}

	return nil
}
开发者ID:packetloop,项目名称:terraform,代码行数:53,代码来源:config.go

示例7: prepareServiceClient

func prepareServiceClient(data *oauthConfig, keyBytes []byte) (
	*http.Client, error) {
	// Craft the ClaimSet and JWT token.
	token := jwt.NewToken(data.Web.Client_Email, oauthScope, keyBytes)
	token.ClaimSet.Aud = data.Web.Token_Uri

	transport, err := jwt.NewTransport(token)
	if err != nil {
		log.Fatal("Assertion error:", err)
	}

	return transport.Client(), nil
}
开发者ID:GrimDerp,项目名称:api-client-go,代码行数:13,代码来源:main.go

示例8: connect

func (c *Client) connect() (*bigquery.Service, error) {
	if c.token != nil {
		fmt.Println("token expired", c.token.Expired())
		fmt.Println("token expiry", c.token.Expiry)

		if !c.token.Expired() && c.service != nil {
			fmt.Println("REUSE SERVICE")
			return c.service, nil
		}
	}

	// generate auth token and create service object
	authScope := bigquery.BigqueryScope
	pemKeyBytes, err := ioutil.ReadFile(c.pemPath)
	if err != nil {
		panic(err)
	}

	t := jwt.NewToken(c.accountEmailAddress, bigquery.BigqueryScope, pemKeyBytes)

	httpClient := &http.Client{}
	token, err := t.Assert(httpClient)
	if err != nil {
		return nil, err
	}

	c.token = token

	config := &oauth.Config{
		ClientId:     c.userAccountClientId,
		ClientSecret: c.clientSecret,
		Scope:        authScope,
		AuthURL:      "https://accounts.google.com/o/oauth2/auth",
		TokenURL:     "https://accounts.google.com/o/oauth2/token",
	}

	transport := &oauth.Transport{
		Token:  token,
		Config: config,
	}

	client := transport.Client()

	service, err := bigquery.New(client)
	if err != nil {
		return nil, err
	}

	c.service = service
	return service, nil
}
开发者ID:nodemeter,项目名称:bigquery,代码行数:51,代码来源:client.go

示例9: NewGCSStorage

func NewGCSStorage(gcsClientEmail, gcsTokenURI string, gcsPemBytes []byte) (Storage, error) {
	t := jwt.NewToken(gcsClientEmail, storage.DevstorageFull_controlScope, gcsPemBytes)
	t.ClaimSet.Aud = gcsTokenURI
	transport, err := jwt.NewTransport(t)
	if err != nil {
		return nil, err
	}
	client := transport.Client()
	gcsService, err := storage.New(client)
	if err != nil {
		return nil, err
	}
	return &GoogleCloudStorage{gcsService}, nil
}
开发者ID:secondbit,项目名称:gifs-api,代码行数:14,代码来源:context.go

示例10: connect

// Helper method to create an authenticated connection.
func connect() (*oauth.Token, *bigquery.Service, error) {
	if *clientId == "" {
		return nil, nil, fmt.Errorf("no client id specified")
	}
	if *serviceAccount == "" {
		return nil, nil, fmt.Errorf("no service account specified")
	}
	if *projectId == "" {
		return nil, nil, fmt.Errorf("no project id specified")
	}
	authScope := bigquery.BigqueryScope
	if *pemFile == "" {
		return nil, nil, fmt.Errorf("no credentials specified")
	}
	pemBytes, err := ioutil.ReadFile(*pemFile)
	if err != nil {
		return nil, nil, fmt.Errorf("could not access credential file %v - %v", pemFile, err)
	}

	t := jwt.NewToken(*serviceAccount, authScope, pemBytes)
	token, err := t.Assert(&http.Client{})
	if err != nil {
		fmt.Printf("Invalid token: %v\n", err)
		return nil, nil, err
	}
	config := &oauth.Config{
		ClientId:     *clientId,
		ClientSecret: *clientSecret,
		Scope:        authScope,
		AuthURL:      "https://accounts.google.com/o/oauth2/auth",
		TokenURL:     "https://accounts.google.com/o/oauth2/token",
	}

	transport := &oauth.Transport{
		Token:  token,
		Config: config,
	}
	client := transport.Client()

	service, err := bigquery.New(client)
	if err != nil {
		fmt.Printf("Failed to create new service: %v\n", err)
		return nil, nil, err
	}

	return token, service, nil
}
开发者ID:DreadPirateShawn,项目名称:heapster,代码行数:48,代码来源:client.go

示例11: GetClient

// GetClient returns an OAuth2 authenticated http.Client that uses the identity
// of the server.
//
// A private key is required to assert the identity.
func (s *ServiceAccount) GetClient(scope string, r http.RoundTripper) (*http.Client, error) {
	if r == nil {
		r = http.DefaultTransport
	}
	tok := jwt.NewToken(s.EmailAddress, scope, []byte(s.PrivateKey))

	// Get the access token right away by doing an HTTP request.
	accessToken, err := tok.Assert(&http.Client{Transport: r})
	if err != nil {
		return nil, fmt.Errorf("failed to assert new oauth2 token: %s", err)
	}
	t := &transport{
		token:       tok,
		accessToken: accessToken,
		projectID:   s.ProjectID,
		transport:   r,
	}
	return &http.Client{Transport: t}, nil
}
开发者ID:maruel,项目名称:ofh,代码行数:23,代码来源:serviceaccount.go

示例12: init

func (s *gcsStorage) init() error {
	jwtToken := jwt.NewToken(os.Getenv(gcsIssEnvVar), gcs.DevstorageRead_writeScope, []byte(os.Getenv(gcsKeyEnvVar)))
	oauthToken, err := jwtToken.Assert(http.DefaultClient)
	if err != nil {
		return err
	}

	client := (&jwt.Transport{jwtToken, oauthToken, http.DefaultTransport}).Client()

	service, err := gcs.New(client)
	if err != nil {
		return err
	}

	s.client = client
	s.service = service
	s.bucket = os.Getenv(gcsBucketEnvVar)

	return nil
}
开发者ID:WIZARD-CXY,项目名称:golang-devops-stuff,代码行数:20,代码来源:storage.go

示例13: GetToken

func (c ApiClient) GetToken() *oauth.Token {

	keyBytes, err := base64.StdEncoding.DecodeString(c.EncodedKey)
	if err != nil {
		log.Fatal("Error decoding private key:", err)
	}

	t := jwt.NewToken(c.ClientId, scope, keyBytes)
	t.ClaimSet.Aud = tokenURL

	log.Print("Requesting new access token.\n")
	httpClient := &http.Client{}
	token, err := t.Assert(httpClient)
	if err != nil {
		log.Fatal("assertion error:", err)
	}

	log.Printf("New access token acquired.\n")
	return token
}
开发者ID:roc,项目名称:situation-room,代码行数:20,代码来源:client.go

示例14: NewDriverGCE

func NewDriverGCE(ui packer.Ui, p string, a *accountFile, c *clientSecretsFile) (Driver, error) {
	// Get the token for use in our requests
	log.Printf("[INFO] Requesting Google token...")
	log.Printf("[INFO]   -- Email: %s", a.ClientEmail)
	log.Printf("[INFO]   -- Scopes: %s", DriverScopes)
	log.Printf("[INFO]   -- Private Key Length: %d", len(a.PrivateKey))
	log.Printf("[INFO]   -- Token URL: %s", c.Web.TokenURI)
	jwtTok := jwt.NewToken(
		a.ClientEmail,
		DriverScopes,
		[]byte(a.PrivateKey))
	jwtTok.ClaimSet.Aud = c.Web.TokenURI
	token, err := jwtTok.Assert(new(http.Client))
	if err != nil {
		return nil, fmt.Errorf("Error retrieving auth token: %s", err)
	}

	// Instantiate the transport to communicate to Google
	transport := &oauth.Transport{
		Config: &oauth.Config{
			ClientId: a.ClientId,
			Scope:    DriverScopes,
			TokenURL: c.Web.TokenURI,
			AuthURL:  c.Web.AuthURI,
		},
		Token: token,
	}

	log.Printf("[INFO] Instantiating GCE client...")
	service, err := compute.New(transport.Client())
	if err != nil {
		return nil, err
	}

	return &driverGCE{
		projectId: p,
		service:   service,
		ui:        ui,
	}, nil
}
开发者ID:JNPRAutomate,项目名称:packer,代码行数:40,代码来源:driver_gce.go

示例15: main

func main() {

	token := jwt.NewToken(Email, Scope, []byte(PEM))
	transport, err := jwt.NewTransport(token)
	if err != nil {
		log.Fatal(err)
	}

	client := transport.Client()
	bq, err := bigquery.New(client)
	if err != nil {
		log.Fatal(err)
	}

	rows := make([]*bigquery.TableDataInsertAllRequestRows, 0)

	row := &bigquery.TableDataInsertAllRequestRows{
		Json: make(map[string]bigquery.JsonValue, 0),
	}
	row.Json["url"] = "https://github.com"
	row.Json["source"] = "example"
	row.Json["t"] = time.Now().Unix()
	row.Json["http_status"] = 200

	rows = append(rows, row)

	req := &bigquery.TableDataInsertAllRequest{
		Rows: rows,
	}

	call := bq.Tabledata.InsertAll(App, Dataset, Table, req)
	resp, err := call.Do()
	if err != nil {
		log.Fatal(err)
	}

	buf, _ := json.Marshal(resp)
	log.Print(string(buf))
}
开发者ID:voidabhi,项目名称:golang-scripts,代码行数:39,代码来源:bigquery.go


注:本文中的code/google/com/p/goauth2/oauth/jwt.NewToken函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。