80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Config struct {
|
|
Token string
|
|
}
|
|
|
|
// Authenticate using OAuth2 password credentials
|
|
func GetAccessToken(c *Config) (string, error) {
|
|
|
|
// // Setup OAuth2 authentication.
|
|
// conf := oauth2.Config{
|
|
// ClientID: c.ClientID,
|
|
// ClientSecret: "",
|
|
// Endpoint: oauth2.Endpoint{
|
|
// TokenURL: "https://" + c.Hostname + "/oidc-token-service/" + c.ClientID + "/token",
|
|
// },
|
|
// Scopes: []string{"em_api_access"},
|
|
// }
|
|
// log.WithFields(log.Fields{"conf": fmt.Sprintf("%+v", conf)}).Debug()
|
|
// token, err := conf.PasswordCredentialsToken(context.TODO(), c.Username, c.Password)
|
|
// if err != nil {
|
|
// log.Error("Error retrieving AccessToken. Check configuration, username and password")
|
|
// return "", err
|
|
// }
|
|
// log.WithFields(log.Fields{"token": fmt.Sprintf("%+v", token.AccessToken)}).Debug()
|
|
// return token.AccessToken, nil
|
|
return c.Token, nil
|
|
}
|
|
|
|
func GetOpportunities(c *Config, url string) (string, error) {
|
|
accessToken, err := GetAccessToken(c)
|
|
|
|
if err != nil {
|
|
log.Error(err)
|
|
return "", err
|
|
}
|
|
|
|
if url == "" {
|
|
url = "https://verint-my.sharepoint.com/personal/peter_morton_verint_com/_api/web/lists/GetByTitle('DFE Focus Opportunities')/items?%24select=Title%2CStatus%2CSolutionConsultantId"
|
|
}
|
|
|
|
method := "GET"
|
|
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest(method, url, nil)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
}
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Prefer", "apiversion=2.1")
|
|
req.Header.Add("Accept", "application/json;odata=verbose")
|
|
req.Header.Add("Authorization", "Bearer "+accessToken)
|
|
// req.Header.Add("Authorization", accessToken)
|
|
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "", err
|
|
}
|
|
log.WithFields(log.Fields{"result.body": fmt.Sprintf("%+v", string(body))}).Debug()
|
|
return string(body), nil
|
|
}
|