func generateAccessToken(privateKey string) (string, error) {

	const tokenEndpoint = "https://oauth2.googleapis.com/token"
	const grantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
	var respBody []byte

	//oAuthAccessToken is a structure to hold OAuth response
	type oAuthAccessToken struct {
		AccessToken string `json:"access_token,omitempty"`
		ExpiresIn   int    `json:"expires_in,omitempty"`
		TokenType   string `json:"token_type,omitempty"`
	}

	token, err := generateJWT(privateKey)

	if err != nil {
		return "", nil
	}

	form := url.Values{}
	form.Add("grant_type", grantType)
	form.Add("assertion", token)

	client := &http.Client{}
	req, err := http.NewRequest("POST", tokenEndpoint, strings.NewReader(form.Encode()))
	if err != nil {
		clilog.Error.Println("error in client: ", err)
		return "", err
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode())))

	resp, err := client.Do(req)

	if err != nil {
		clilog.Error.Println("failed to generate oauth token: ", err)
		return "", err
	}

	if resp != nil {
		defer resp.Body.Close()
	}

	if resp == nil {
		clilog.Error.Println("error in response: Response was null")
		return "", errors.New("error in response: Response was null")
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		clilog.Error.Println("error in response: ", err)
		return "", fmt.Errorf("error in response: ", err)
	} else if resp.StatusCode > 399 {
		clilog.Error.Printf("status code %d, error in response: %s\n", resp.StatusCode, string(respBody))
		return "", fmt.Errorf("status code %d, error in response: %s\n", resp.StatusCode, string(respBody))
	}

	accessToken := oAuthAccessToken{}
	if err = json.Unmarshal(respBody, &accessToken); err != nil {
		return "", err
	}

	clilog.Info.Println("access token : ", accessToken)

	SetApigeeToken(accessToken.AccessToken)
	_ = WriteToken(accessToken.AccessToken)
	return accessToken.AccessToken, nil
}
//generateAccessToken generates a Google OAuth access token from a service account
generateAccessToken generates a Google OAuth access token from a service account
// generateAccessToken generates a Google OAuth access token from a service account
func generateAccessToken(privateKey string) (string, error) {
	const tokenEndpoint = "https://oauth2.googleapis.com/token"
	const grantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
	var respBody []byte
	type oAuthAccessToken struct {
		AccessToken string `json:"access_token,omitempty"`
		ExpiresIn   int    `json:"expires_in,omitempty"`
		TokenType   string `json:"token_type,omitempty"`
	}
	token, err := generateJWT(privateKey)
	if err != nil {
		return "", nil
	}
	form := url.Values{}
	form.Add("grant_type", grantType)
	form.Add("assertion", token)
	client := &http.Client{}
	req, err := http.NewRequest("POST", tokenEndpoint, strings.NewReader(form.Encode()))
	if err != nil {
		clilog.Error.Println("error in client: ", err)
		return "", err
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode())))
	resp, err := client.Do(req)
	if err != nil {
		clilog.Error.Println("failed to generate oauth token: ", err)
		return "", err
	}
	if resp != nil {
		defer resp.Body.Close()
	}
	if resp == nil {
		clilog.Error.Println("error in response: Response was null")
		return "", errors.New("error in response: Response was null")
	}
	respBody, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		clilog.Error.Println("error in response: ", err)
		return "", fmt.Errorf("error in response: ", err)
	} else if resp.StatusCode > 399 {
		clilog.Error.Printf("status code %d, error in response: %s\n", resp.StatusCode, string(respBody))
		return "", fmt.Errorf("status code %d, error in response: %s\n", resp.StatusCode, string(respBody))
	}
	accessToken := oAuthAccessToken{}
	if err = json.Unmarshal(respBody, &accessToken); err != nil {
		return "", err
	}
	clilog.Info.Println("access token : ", accessToken)
	SetApigeeToken(accessToken.AccessToken)
	_ = WriteToken(accessToken.AccessToken)
	return accessToken.AccessToken, nil
}
func generateAccessToken ( privateKey string ) ( string , error ) { const tokenEndpoint = " " const grantType = " " var respBody [ ] byte type oAuthAccessToken struct { AccessToken string `json:"access_token,omitempty"` ExpiresIn int `json:"expires_in,omitempty"` TokenType string `json:"token_type,omitempty"` } token , err := generateJWT ( privateKey ) if err != nil { return " " , nil } form := url . Values { } form . Add ( " " , grantType ) form . Add ( " " , token ) client := & http . Client { } req , err := http . NewRequest ( " " , tokenEndpoint , strings . NewReader ( form . Encode ( ) ) ) if err != nil { clilog . Error . Println ( " " , err ) return " " , err } req . Header . Add ( " " , " " ) req . Header . Add ( " " , strconv . Itoa ( len ( form . Encode ( ) ) ) ) resp , err := client . Do ( req ) if err != nil { clilog . Error . Println ( " " , err ) return " " , err } if resp != nil { defer resp . Body . Close ( ) } if resp == nil { clilog . Error . Println ( " " ) return " " , errors . New ( " " ) } respBody , err = ioutil . ReadAll ( resp . Body ) if err != nil { clilog . Error . Println ( " " , err ) return " " , fmt . Errorf ( " " , err ) } else if resp . StatusCode > 399 { clilog . Error . Printf ( " \n " , resp . StatusCode , string ( respBody ) ) return " " , fmt . Errorf ( " \n " , resp . StatusCode , string ( respBody ) ) } accessToken := oAuthAccessToken { } if err = json . Unmarshal ( respBody , & accessToken ) ; err != nil { return " " , err } clilog . Info . Println ( " " , accessToken ) SetApigeeToken ( accessToken . AccessToken ) _ = WriteToken ( accessToken . AccessToken ) return accessToken . AccessToken , nil }
generateAccessToken generates a Google OAuth access token from a service account
generateAccessToken generates a Google OAuth access token from a service account
//oAuthAccessToken is a structure to hold OAuth response