This is just a collection of functions written in different languages to create an access_token using your client credentials. It is meant to help people who are starting now with the API.
There are very good community created libraries that already deal with this OAuth flow and all the endpoint requests. Use this only if you want to create your own implementation or understand better how it works.
PHP
function createAccessToken($apiKey, $apiSecret, $region = 'us') {
$curl_handle = curl_init();
try {
curl_setopt($curl_handle, CURLOPT_URL, "https://$region.battle.net/oauth/token");
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, ['grant_type' => 'client_credentials']);
curl_setopt($curl_handle, CURLOPT_USERPWD, $apiKey . ':' . $apiSecret);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
$response = curl_exec($curl_handle);
$status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
if ($status !== 200) {
throw new Exception('Failed to create client_credentials access token.');
}
return json_decode($response)->access_token;
} finally {
curl_close($curl_handle);
}
}
GScript (Google docs/spreadsheets)
function createAccessToken(region, ApiKey, ApiSecret) {
var formData = {
'grant_type': 'client_credentials'
};
var options = {
'method': 'post',
'payload': formData,
'headers': {
'Authorization': 'Basic ' + Utilities.base64Encode(ApiKey + ':' + ApiSecret)
}
};
var url = 'https://' + region + '.battle.net/oauth/token';
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response.getContentText()).access_token;
}
Ruby
def create_access_token(client_id, client_secret, region = 'us')
uri = URI.parse("https://#{region}.battle.net/oauth/token")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path)
request.basic_auth(client_id, client_secret)
request['Content-Type'] = 'application/x-www-form-urlencoded'
request.set_form_data grant_type: 'client_credentials'
response = http.request(request)
JSON.parse(response.body)['access_token']
end
Go
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
func CreateAccessToken(clientID string, clientSecret string, region string) string {
requestURL := fmt.Sprintf("https://%s.battle.net/oauth/token", region)
body := strings.NewReader("grant_type=client_credentials")
request, err := http.NewRequest(http.MethodPost, requestURL, body)
if err != nil {
log.Fatal(err)
}
request.SetBasicAuth(clientID, clientSecret)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
httpClient := new(http.Client)
response, err := httpClient.Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
responseData, err := ioutil.ReadAll(response.Body)
var tokenData Token
err = json.Unmarshal(responseData, &tokenData)
if err != nil {
log.Fatal(err)
}
return tokenData.AccessToken
}