Overfloweth
Overfloweth

Reputation: 41

M Language - How to get JSON in HTTP Request Body? (Vimeo API Unsupported Grant Type Error)

I am attempting to create my first PowerBI Custom Connecter to connect to the Vimeo API. I am stuck on the final step of the authorization flow - getting back an access token. When trying out the Connecter in PowerBI, it seems to authenticate properly when I hit the access token endpoint, but I get back a warning "[unsupported_grant_type] Unsupported grant type"

It appears I am not sending the grant_type properly in the request. Here are Vimeo's requirements of what is sent along in the header and body of the request:


Header Set value to
Authorization basic base64_encode(x:y), where x is the client identifier and y is the client secret
Content-Type application/json
Accept application/vnd.vimeo.*+json;version=3.4

"In the request body, send the grant_type field with the value authorization_code. You must also set code to the authorization code string that you just received and redirect_uri to the redirect URI that you specified previously — don't use a different redirect URI."

{
  "grant_type": "authorization_code",
  "code": "{code}",
  "redirect_uri": "{redirect_uri}"
}

Here is a snippet of code from the Customer Connector I am building. It is within this TokenMethod function that I am trying to fulfill the requirements of the table above. I am getting the sense I am not correctly placing the JSON in the body of the request, but I am stuck on what to try next:


TokenMethod = (grantType, tokenField, code) =>
   let
       queryString = [
           grant_type = "authorization_code",
           redirect_uri = redirect_uri,
           client_id = client_id,
           client_secret = client_secret
       ],
       queryWithCode = Record.AddField(queryString, tokenField, code),

       authKey = "Basic " & Binary.ToText(Text.ToBinary("client_id:client_secret"),BinaryEncoding.Base64),

       tokenResponse = Web.Contents(token_uri, [
           Content = Text.ToBinary(Uri.BuildQueryString(queryWithCode)),
           Headers = [
               #"Authorization" = authKey,
               #"Content-type" = "application/json",
               #"Accept" = "application/vnd.vimeo.*+json;version=3.4"
           ],
           ManualStatusHandling = {400} 
       ]),
       body = Json.Document(tokenResponse),
       result = if (Record.HasFields(body, {"error", "error_description"})) then 
                   error Error.Record(body[error], body[error_description], body)
                else
                   body
   in
       result;

I'm wondering if someone could please point out where I might be going astray in the code and why I am receiving the [unsupported_grant_type] error.

Many thanks for your time!

Upvotes: 0

Views: 268

Answers (1)

Overfloweth
Overfloweth

Reputation: 41

I changed Content-Type to "application/x-www-form-urlencoded" and it worked!

Upvotes: 0

Related Questions