Membrane API Gateway returns 502 Bad Gateway with the detail Could not obtain an OAuth2 access token. when the oauth2Client plugin cannot get an access token from the authorization server. The request is aborted before it reaches the backend. The client is not at fault.
The response contains the status 502 Bad Gateway, the title Bad Gateway, the detail Could not obtain an OAuth2 access token., the problem type https://membrane-api.io/problems/gateway and a link to this page. The type gateway means that the problem lies between Membrane and an upstream system, not in the client's request.
The oauth2Client plugin fetches an access token from the tokenUrl using the client credentials flow and adds it to the request as an Authorization: Bearer header. A token is cached until it expires, when the authorization server sends expires_in.
api:
port: 2000
flow:
- oauth2Client:
tokenUrl: https://auth.example.com/oauth2/token
clientId: gateway
clientSecret: secret
scope: orders
target:
url: https://api.example.com
api:
port: 2000
flow:
- oauth2Client:
tokenUrl: https://auth.example.com/oauth2/token
clientId: gateway
clientSecret: secret
scope: orders
target:
url: https://api.example.com
The token endpoint answers with a status other than 200, for example 401 because clientId or clientSecret is wrong, or 400 because the requested scope is not allowed for this client.
The response of the token endpoint is not JSON, has no access_token field, or the field is empty.
The host is unknown, the connection is refused, or the request times out or fails in the TLS handshake. The token request uses the global HTTP client configuration of the gateway (configuration.httpClientConfig), including its proxy settings.
A common mistake is to configure the authorization endpoint instead of the token endpoint.
info level with the reason, and the full exception at debug level.curl using the same tokenUrl, clientId, clientSecret and scope to see the answer of the authorization server.configuration.httpClientConfig.proxy.Clients can retry the request once the problem is fixed.
By default, Membrane runs in development mode and adds details to error responses that help you find the problem, including the link to this page. These details may reveal information about your setup. In production, turn them off:
configuration:
production: trueconfiguration: production: true
Add the configuration document to your apis.yaml and separate it from the APIs with ---.
In production mode, the title of this error is replaced by Internal Server Error, and the response only says Internal details are hidden. See server log (key: ...). Search the gateway log for that key to find the full error message.
Configuration reference:
Tutorials with runnable examples:
Still stuck? See the troubleshooting guide or ask on GitHub Issues. For commercial support, contact info@predic8.de.