Skip to content
Advertisement

How to get an OAuth 2.0 token in PHP?

I need to connect to a REST API of a customer. The first step is to get an OAuth 2.0 access token. I got the following information from the customer:

  • Login (I assume it’s the client_id)
  • Password (I assume it’s the client_secret)
  • The flow is password
  • The regular API URL
  • The Token API URL

I found code at http://tutorialspage.com/simple-oauth2-example-using-php-curl/ and changed it, just for testing, to this:

JavaScript

I would expect to get a JSON response containing valid OAuth 2.0 access_token, but instead I get an error:

JavaScript

How to change the code to make it work?

Edit: It works over the shell-curl on my mac:

JavaScript

Verbose Informations Curl (MacShell):

JavaScript

Verbose Informstions cURL PHP:

JavaScript

I didn’t find the problem.

Advertisement

Answer

OK, thanks to Helen it works now.

JavaScript

The Problem was solved as I entered the Post Data as String: CURLOPT_POSTFIELDS => "grant_type=password&username=username@example.foo&password=123and4" Instead of a array:

JavaScript

Thanks <3!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement