locked
Rest API Post using Invoke-RestMethod RRS feed

  • Question

  • My power shell below errors saying my Invoke Rest Method has a bad Request? Any ideas how to get my request correct? My instructions (screen capture... Here is how you do it.) are below:

    $body = @{
     "grant_type"="refresh_token"
     "client_id"="87bbaccd-cb8f366e"
     "client_secret"="515b2589"
     "client_token"="rqD6qcwDQPntmpmr9hf0TEES1"
    } | ConvertTo-Json
    $header = @{
     "Content-Type"="application/x-www-form-urlencoded"
    }
    Invoke-RestMethod -Uri "https://api2.ibmmarketingcloud.com/oauth/token" -Method 'Post' -Body $body -ContentType "application/x-www-form-urlencoded"


    Regards,

    Ryan P. Casey, MBA
    SSAS, SSIS, SSRS Architect

    Website and Blog
    http://www.aimYourData.com
    http://www.rhynoData.com

    YouTube Channel
    https://www.youtube.com/channel/UCd4RTLBn8sjVR5D3gfJhq9Q

    LinkedIn Profile and LinkedIn Group
    http://www.linkedin.com/in/ryancaseymba
    https://www.linkedin.com/groups/7065009

    Twitter
    http://www.twitter.com/RyanCaseyMBA

    Microsoft MVP
    https://mvp.microsoft.com/en-us/PublicProfile/5002566?fullName=Ryan%20Patrick%20Casey

    Friend of Redgate
    https://www.red-gate.com/hub/events/friends-of-rg/friend/ryan.casey

    Thursday, January 31, 2019 5:32 PM

All replies

  • You header is probably incorrect.  It should be "application/json" from what I can see.

    You should post this in the IBM forum since it is their API.


    \_(ツ)_/

    Thursday, January 31, 2019 6:14 PM
  • I tried this, but it is not the issue. Thanks.

    I have also posted in IBM, but I wanted to give PowerShell experts a look too.


    Regards,

    Ryan P. Casey, MBA
    SSAS, SSIS, SSRS Architect

    Website and Blog
    http://www.aimYourData.com
    http://www.rhynoData.com

    YouTube Channel
    https://www.youtube.com/channel/UCd4RTLBn8sjVR5D3gfJhq9Q

    LinkedIn Profile and LinkedIn Group
    http://www.linkedin.com/in/ryancaseymba
    https://www.linkedin.com/groups/7065009

    Twitter
    http://www.twitter.com/RyanCaseyMBA

    Microsoft MVP
    https://mvp.microsoft.com/en-us/PublicProfile/5002566?fullName=Ryan%20Patrick%20Casey

    Friend of Redgate
    https://www.red-gate.com/hub/events/friends-of-rg/friend/ryan.casey

    Thursday, January 31, 2019 6:19 PM
  • Only IBM can tell you what you are missing.

    You can also try "text/json".


    \_(ツ)_/

    Thursday, January 31, 2019 6:23 PM
  • This may be your error:

    $body = @{
      grant_type = 'refresh_token'
      client_id = '87bbaccd-cb8f366e'
      client_secret = '515b2589'
      client_token = 'rqD6qcwDQPntmpmr9hf0TEES1'
     } | ConvertTo-Json
    
    $headers = @{
      'Content-Type' = 'application/x-www-form-urlencoded'
    }
    
    Invoke-RestMethod -Uri https://api2.ibmmarketingcloud.com/oauth/token' -Method Post -Body $body -Headers $header


    \_(ツ)_/







    • Edited by jrv Thursday, January 31, 2019 6:31 PM
    Thursday, January 31, 2019 6:26 PM
  • Why are you converting to JSON if the content type is URL encode?  You can URL encode in PowerShell using a .NET library.

    Add-Type -AssemblyName System.Web

    $urlEncodedBody = [System.Web.HttpUtility]::UrlEncode($body)

    Invoke-RestMethod -Uri "https://api2.ibmmarketingcloud.com/oauth/token" `

    -Method 'Post' -Body $urlEncodedBody -ContentType "application/x-www-form-urlencoded"

    Every API is going to be different so you'll need to get more documentation on the exact format required.

    Friday, February 1, 2019 3:12 PM
  • I took a closer look at this and your main error is "client_token" should be "refresh_token" and you do not need to convert to JSON for the body

    $body = @{
     "grant_type"="refresh_token"
     "client_id"="87bbaccd-cb8f366e"
     "client_secret"="515b2589"
     "refresh_token"="rqD6qcwDQPntmpmr9hf0TEES1"
     }
     
    Invoke-RestMethod -Uri "https://api2.ibmmarketingcloud.com/oauth/token" -Method 'Post' -Body $Body -ContentType "application/x-www-form-urlencoded" 
    That will return a JSON response from the API indicating the client ID is not valid (I assume you did that intentionally).  But the syntax is correct and you'll just need to correct the inputs.  As for the URL encoding, again every API is different - they must want just the parameter name and value encoded but for example not the equal sign.  I don't think I've ever seen a URL encode API format that was ever the same.  In your example there are no special characters in the name or value, and thus the raw string is equal to the URL encoded string.  I'd assume you'd have to encode if special characters do come into play, so you may still need to account for that.

    Friday, February 1, 2019 5:15 PM