I am using below AMPScript to retrieve access token for my subsequent REST calls but it is failing with "Bad Request"

%%[var @con,@body,@body1,@auth,@output,@response,@res,@callstatus ]%% 
%%[SET @auth="Bearer xxxxxxxxxxxxxx" 
SET @body="{"clientId":"xxxxx","clientSecret":"yyyyyyy"}"
HTTPPost("https://auth.exacttargetapis.com/v1/requestToken","application/json",@body,@callstatus) ]%% 

I suggest using single quotes for the string and double quotes in the json of your body-variable. Additionally I suggest using HTTPPost2 as you need to use the returned body afterwards.

    Var @body, @callstatus, @response
    Set @body = '{"clientId": "xxxxx", "clientSecret": "yyyyyyy"}'

    HTTPPost2("https://auth.exacttargetapis.com/v1/requestToken", "application/json", @body, True, @callstatus, @response)


In the subsequent requests you need to add another parameter containing "Authorization" and then another one containing "Bearer [YOUR TOKEN FROM RESPONSE]".

Further reading:

  • Hi Marcus, I am still getting the same error Unable to generate preview The function expression is invalid. See inner exception for detail. Script: HTTPPost2("https://auth.exacttargetapis.com/v1/requestToken", "application/json", @body, True, @callstatus, @response) Index: 6971 ListID: 0 An error occurred when attempting to evaluate a HTTPPost2 function call. See inner exception for details. The remote server returned an error: (400) Bad Request. – Maneesh Jun 16 '17 at 18:40
  • Did you try to send this request using curl command line tool or postman chrome extension for example? Just to verify the credentials definitely work. – Markus Slabina Jun 16 '17 at 18:42
  • Yes I copied these from my postman only .. so credentials positively work – Maneesh Jun 16 '17 at 18:42
  • Just another try: HTTPPost2("https://auth.exacttargetapis.com/v1/requestToken", "application/json", @body, True, @callstatus, @response, "Content-Type", "application/json; charset=utf-8") additionally also try this for your body (single quotes for string and unescaped double quotes for json): Set @body = '{"clientId": "xxxxx", "clientSecret": "yyyyyyy"}' – Markus Slabina Jun 16 '17 at 18:46
  • 1
    @DigitalMC You need to create a new installed package and get the credentials from there. In the past this was done in the AppCenter, but now this can be done within the Marketing Cloud interface. See Install Packages and API Integration documentation pages for more information regarding this. – Markus Slabina Mar 26 '18 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.