Issues with connected App - {"error_description":"authentication failure","error":"invalid_grant"}
I feel pretty dumb answering my own question but that may help somebody someday.
@Rao was totally right about that one so he deserves all the credit.
In my "Edit 1", I was wrong about the content of $instance.
It was not pointing to 'https://test.salesforce.com', it was pointing to 'https://login.salesforce.com' so it was normal to get an "authentication failure" error.
So if you're experiencing the same problem, do check the URL you're sending the request to.
Another thing that was not obvious to me from the documentation: the host URL is not just either "test.salesforce.com" or "login.salesforce.com". Mine is "na30.salesforce.com".
After I changed it from the default "test.salesforce.com" used in all the documentation I've read so far, OAuth worked, where before it would give me the "invalid grant" error.
I might have skipped some introductory materials...
PS: I don't have enough rep to post actual links. The above URLs all need the https:// prefix, of course.
It's important to check that the policies for the connected api you are using to authenticate with have Permitted Users
equal All users may self-authorize
.
To check, in lightning experience go to:
Setup > App Manager > yourConnectedAppName > click the down arrow on the far right > Manage > Edit Policies