Mastercard / terraform-provider-restapi

A terraform provider to manage objects in a RESTful API
Other
808 stars 217 forks source link

The request body must contain the following parameter: 'grant_type' #232

Open Ramguru94 opened 1 year ago

Ramguru94 commented 1 year ago

unexpected response code '400': {"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'grant_type'.

I am trying to create Bearer token for a Azure service principal.

Provider.tf contains

provider "restapi" {
  alias = "restapi_bearer"
  uri   = "https://login.microsoftonline.com/"
  headers = {
    Content-Type = "application/json"
  }
  create_method        = "POST"
  write_returns_object = true

}

resource.tf contains

resource "restapi_object" "sp" {
  path = "/${azuread_service_principal.tenant_sp.application_tenant_id}/oauth2/v2.0/token"
  data = jsonencode({
    grant_type    = "client_credentials"
    client_id     = "${azuread_service_principal.tenant_sp.application_id}"
    scope         = "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default"
    client_secret = "${azuread_application_password.tenant_sp.value}"
  })
}

My data has grant_type set and still i am getting 400

Am i missing something or doing this wrong!

Ramguru94 commented 1 year ago
2023-08-17T16:10:41.860+0200 [INFO]  provider.terraform-provider-restapi_v1.18.1: 2023/08/17 16:10:41 api_client.go: Request headers:: timestamp=2023-08-17T16:10:41.860+0200
2023-08-17T16:10:41.860+0200 [INFO]  provider.terraform-provider-restapi_v1.18.1: 2023/08/17 16:10:41 api_client.go:   Content-Type: application/json: timestamp=2023-08-17T16:10:41.860+0200
2023-08-17T16:10:41.860+0200 [INFO]  provider.terraform-provider-restapi_v1.18.1: 2023/08/17 16:10:41 api_client.go: BODY:: timestamp=2023-08-17T16:10:41.860+0200

the BODY part looks empty ?

majosula commented 7 months ago

I am fighting with the very same problem. Did you find a solution for this?

resource "restapi_object" "get_token" { path = "/api/oauth2/token" debug = true data = jsonencode({ grant_type = "password" username = var.user password = var.pwd refresh_token = "" code = "" use_short_term_refresh = "" vbr_token = "" }) }

Error: unexpected response code '400': { │ "errorCode": 0, │ "message": "TokenLoginSpec.GrantType : 'Grant Type' must not be empty. ", │ "resourceId": null │ } │ │ with restapi_object.get_token, │ on mastercard.tf line 22, in resource "restapi_object" "get_token": │ 22: resource "restapi_object" "get_token" {