Created
October 26, 2018 18:05
-
-
Save esell/b3a94f0b4275fff9d2cfbbc32e1c0a75 to your computer and use it in GitHub Desktop.
blah.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func httpReq(authToken string, method string, URL string, postData []byte, isJson bool) (*http.Response, error) { | |
client := &http.Client{ | |
Timeout: 20 * time.Second, | |
} | |
req, err := http.NewRequest(method, URL, bytes.NewBuffer(postData)) | |
if err != nil { | |
log.Println("Error with building request for "+URL+": ", err) | |
return &http.Response{}, err | |
} | |
if authToken != "" { | |
req.Header.Add("Authorization", "Bearer "+authToken) | |
} | |
if isJson { | |
req.Header.Add("Content-Type", "application/json") | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Println("Error with request for "+URL+": ", err) | |
return &http.Response{}, err | |
} | |
return resp, nil | |
} | |
func getAuthToken(clientID, clientSecret, tenantName string) (AuthTokenResp, error) { | |
data := url.Values{} | |
data.Set("grant_type", "client_credentials") | |
data.Add("client_id", parsedconfig.ClientID) | |
data.Add("client_secret", parsedconfig.ClientSecret) | |
data.Add("resource", "https://management.azure.com/") | |
r, _ := httpReq("", "POST", "https://login.microsoftonline.com/"+parsedconfig.TenantName+"/oauth2/token", []byte(data.Encode()), false) | |
var authResp AuthTokenResp | |
defer r.Body.Close() | |
body, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
return authResp, err | |
} | |
err = json.Unmarshal(body, &authResp) | |
if err != nil { | |
return authResp, err | |
} | |
return authResp, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment