# How to get token Machine to Machine with php Auth0 sdk and not curl?

**URL:** <https://community.auth0.com/t/how-to-get-token-machine-to-machine-with-php-auth0-sdk-and-not-curl/170054>\
**Category:** Get Help\
**Tags:** free-trial, connections\
**Created:** [January 24, 2025, 7:53am UTC](https://community.auth0.com/t/how-to-get-token-machine-to-machine-with-php-auth0-sdk-and-not-curl/170054 "2025-01-24T07:53:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin.schmitt.upjv](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@kevin.schmitt.upjv](https://community.auth0.com/u/kevin.schmitt.upjv)\
**Post date:** [January 24, 2025, 7:53am UTC](https://community.auth0.com/t/how-to-get-token-machine-to-machine-with-php-auth0-sdk-and-not-curl/170054/1 "2025-01-24T07:53:31Z")

</div>

Hello everyone,

I need help, please. I’m using a free trial to test Auth0, and I want to test Machine-to-Machine authentication to obtain a token.

The following cURL request from the Auth0 dashboard works correctly, and I receive the token:

```auto
 $curl = curl_init();

            curl_setopt_array($curl, array(
            CURLOPT_URL => "https://dev-2qs4dcflvo4nycaq.us.auth0.com/oauth/token",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "{\"client_id\":\"client_id\",\"client_secret\":\"client_secret\",\"audience\":\"http://api-gateway.localhost/\",\"grant_type\":\"client_credentials\"}",
            CURLOPT_HTTPHEADER => array(
                "content-type: application/json"
            ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
            echo "cURL Error #:" . $err;
            } else {
             echo $response;
            }

```

However, I want to use the Auth0 PHP SDK for this, but it seems impossible because I keep getting errors. It’s complicated, and I receive an **Unauthorized 401** error even though I use the same credentials.

Here is my Auth0 SDK implementation:

```auto
  $auth0 = new Auth0([
                'domain' => 'dev-2qs4dcflvo4nycaq.us.auth0.com',
                'clientId' => 'client_id',
                'clientSecret' => 'client_secret,
                'grant_type' => 'client_credentials',
                // 'strategy' => SdkConfiguration::STRATEGY_API,
                'audience' => ['http://api-gateway.localhost/'],
     'cookieSecret' => 'test'
            ]);
            dd($auth0->authentication()->clientCredentials([
                'audience' => 'http://api-gateway.localhost/'
            ]));

```

I get a **401 Unauthorized** error, and there is no useful information in the Auth0 monitoring dashboard.

I’ve tested different functions…

Do you have any idea what the problem could be?

Thanks!

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 24, 2025, 7:50pm UTC](https://community.auth0.com/t/how-to-get-token-machine-to-machine-with-php-auth0-sdk-and-not-curl/170054/3 "2025-01-24T19:50:33Z")

</div>

Hi @kevin.schmitt.upjv,

Your code looks correct by calling the `clientCredentials()` method to get an access token for your M2M application.

If you are getting 401 Unauthorized error, I suggest ensuring all the values you provided are correct, such as the `clientID` and `clientSecret`.

Here are some references on getting a token for your M2M app using the Auth0 PHP SDK:

- [GitHub - auth0/auth0-PHP: PHP SDK for Auth0 Authentication and Management APIs.](https://github.com/auth0/auth0-PHP?tab=readme-ov-file#add-login-to-your-application)
- [Auth0 PHP SDK - clientCredentials()](https://auth0.github.io/auth0-PHP/classes/Auth0-SDK-API-Authentication.html#method_clientCredentials)

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 7, 2025, 7:51pm UTC](https://community.auth0.com/t/how-to-get-token-machine-to-machine-with-php-auth0-sdk-and-not-curl/170054/4 "2025-02-07T19:51:26Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
