Thursday, 13 April 2017

Turn my Golang into NodeJS OAuth2 Token request

I use GO and one of it's packages (golang.org/x/oauth2) to connect and receive a bearer token for an API which I have to use for work.

Below is the code I use to retrieve the bearer token (written in GO).

Can someone please show me how to achieve the same effect but instead using Node/Javascript.

package main

import (
    "golang.org/x/oauth2"
    "fmt"
)

func main() {
        conf := &oauth2.Config{
               ClientID:     "My-Application-ClientID",
               ClientSecret: "My-Application-Client-Secret",
               Scopes: []string{
                      "openid profile",
               },
               Endpoint: oauth2.Endpoint{
                      AuthURL:  "https://example.com/authorize",
                      TokenURL: "https://example.com/token",
               },
        }
        tok, err := conf.PasswordCredentialsToken(oauth2.NoContext, "My-Service-Account-AccessKey", "My-Service-Account-SecretKey")
        if err != nil {
              fmt.Println(err)
        }
        fmt.Println(tok.AccessToken)
}



via simplymarcello

No comments:

Post a Comment