oAuth2 how to get bearer token?

Hello,

I have to implement an oAuth2 service API with n8n (I’m new to n8n).
The service needs oAuth2 authentification. I put all the parameters in oAuth2 credential (authorization endpoint, token endpoint, client id, and secret), and could connect to the service. Grant type for this service is authorization_code, so from what I read it is compatible with oAuth2 credential implementation of n8n.
However, in the API requests, I need to put the OAuth Bearer Token, which should be returned in the JSON returned by the token endpoint.
How can I access this OAuth Bearer Token so I can use it in the API calls?

What is the error message (if any)?

Without the OAuth Bearer Token I get authorization error when calling APIs.

Information on your n8n setup

  • n8n version: 0.168.2
  • Database you’re using (default: SQLite): SQLite
  • Running n8n with the execution process [own(default), main]: own
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker (with Python support)

Thanks for your help !

Hi @looloo06, welcome to the community :tada:

OAuth2 can be very painful, but n8n should usually attach the required token automatically, meaning if you’re running into an error chances are something else is wrong with the request.

You can still take a look at your token if needed though. If you have configured OAuth2 in your HTTP Request node like so:

Maybe you can share some more information on the service you are trying to connect to, your credentials set up and the node you are using?
You could send a request to a URL provided by (for example) https://webhook.site/. On this page you’d be able to inspect the full request, including the authorization header containing your bearer token:

Hello,

Thanks a lot for your answer.

Here are the details:

Following your answer, I made some tests and finished by having a result “unknown, invalid, or expired refresh token”. I wanted to reconnect in order to get fresh token, and I don’t know why, when reconnecting and after having input the login and password, the redirect URL displays the following result instead of taking into account the connexion (as it was working with my previous tests):
{“code”:0,“message”:“HTTP status 401”,“hint”:""}

PS : redirect URL is the good one.
Capture d’écran 2022-03-21 184440

So I’m stuck now on this identification process, which was working correctly before…

Hi again,

After having recreated a new application ID, I managed to get the data as expected.
Don’t know exactly what did not work before…