# Can't log via API

**URL:** <https://forum.mautic.org/t/cant-log-via-api/10192>\
**Category:** Development\
**Created:** [February 2, 2018, 9:39am UTC](https://forum.mautic.org/t/cant-log-via-api/10192 "2018-02-02T09:39:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phalanx](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phalanx](https://forum.mautic.org/u/phalanx)\
**Post date:** [February 2, 2018, 9:39am UTC](https://forum.mautic.org/t/cant-log-via-api/10192/1 "2018-02-02T09:39:04Z")

</div>

Hi, I can’t log in via API. I am trying to use OAuth2 from [https://github.com/mautic/api-library](https://github.com/mautic/api-library)  
But it always ends on 401 - API authorization denied. Could anyone help me please? I am trying to solve this problem about 8 hours now, so it’s amazingly frustrating.

PHP 7.1  
Mautic v2.12.1 - installed on localhost

```auto

<?php

// Bootup the Composer autoloader
include __DIR__. '/vendor/autoload.php';  

use Mautic\Auth\ApiAuth;

session_start();

$publicKey = '';
$secretKey = '';
$callback = '';

// ApiAuth->newAuth() will accept an array of Auth settings
$settings = array(
    'baseUrl' => 'http://mautic.lh', // Base URL of the Mautic instance
    'version' => 'OAuth2', // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
    'clientKey' => '1_14ky13whdfggsw8oc0ow8oks8ks80cs4oocs0kwco88go4c84c', // Client/Consumer key from Mautic
    'clientSecret' => '15kn10uoa6bk8oc08048sg0444c4g04o0gkk008cs40c8oc4ss', // Client/Consumer secret key from Mautic
    'callback' => 'http://myproject.lh' // Redirect URI/Callback URI for this script
);

/*
// If you already have the access token, et al, pass them in as well to prevent the need for reauthorization
$settings['accessToken'] = $accessToken;
$settings['accessTokenSecret'] = $accessTokenSecret; //for OAuth1.0a
$settings['accessTokenExpires'] = $accessTokenExpires; //UNIX timestamp
$settings['refreshToken'] = $refreshToken;
*/

// Initiate the auth object
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);

// Initiate process for obtaining an access token; this will redirect the user to the $authorizationUrl and/or
// set the access_tokens when the user is redirected back after granting authorization

// If the access token is expired, and a refresh token is set above, then a new access token will be requested
/*
try {
    if ($auth->validateAccessToken()) {

        // Obtain the access token returned; call accessTokenUpdated() to catch if the token was updated via a
        // refresh token

        // $accessTokenData will have the following keys:
        // For OAuth1.0a: access_token, access_token_secret, expires
        // For OAuth2: access_token, expires, token_type, refresh_token

        if ($auth->accessTokenUpdated()) {
            $accessTokenData = $auth->getAccessTokenData();

            //store access token data however you want

	        dump($accessTokenData);
        }
    }
} catch (Exception $e) {
	// Do Error handling
	dump($e);
}
*/

$api = new \Mautic\MauticApi();
$contactApi = $api->newApi('contacts', $auth, $settings['baseUrl'] . '/api');

$fields = $contactApi->getFieldList();

$data = array();

$data['email'] = 'some@email.com';

// Set the IP address the contact originated from if it is different than that of the server making the request
$data['ipAddress'] = '';

// Create the contact
$response = $contactApi->create($data);
var_dump($response);

/**
 array (size=3)
'errors' =>
array (size=1)
0 =>
array (size=3)
'message' => string 'API authorization denied.' (length=25)
'code' => int 401
'type' => string 'access_denied' (length=13)
'error' => string 'access_denied' (length=13)
'error_description' => string 'API authorization denied. (<code>error</code> and <code>error_description</code> are deprecated as of 2.6.0 and will be removed in 3.0. Use the <code>errors</code> array instead.)' (length=146)
 */

// $contact = $response[$contactApi->itemName()];

```

---

<div class="post-metadata">

**Author:** ![bizcrony](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/bizcrony/32/409_2.png) [@bizcrony](https://forum.mautic.org/u/bizcrony)\
**Post date:** [February 2, 2018, 10:13am UTC](https://forum.mautic.org/t/cant-log-via-api/10192/2 "2018-02-02T10:13:44Z")

</div>

Where you want to integrate this API ?

---

<div class="post-metadata">

**Author:** ![phalanx](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phalanx](https://forum.mautic.org/u/phalanx)\
**Post date:** [February 2, 2018, 10:30am UTC](https://forum.mautic.org/t/cant-log-via-api/10192/3 "2018-02-02T10:30:55Z")

</div>

Both projects are on localhost - my dev notebook

---

<div class="post-metadata">

**Author:** ![phalanx](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phalanx](https://forum.mautic.org/u/phalanx)\
**Post date:** [February 2, 2018, 3:16pm UTC](https://forum.mautic.org/t/cant-log-via-api/10192/4 "2018-02-02T15:16:16Z")

</div>

Problem solved - callback\_url was different in mautic and app (they must be the same…!)

---

<div class="post-metadata">

**Author:** ![sarthakinfotech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/sarthakinfotech/32/336_2.png) [@sarthakinfotech](https://forum.mautic.org/u/sarthakinfotech)\
**Post date:** [June 9, 2018, 10:56pm UTC](https://forum.mautic.org/t/cant-log-via-api/10192/5 "2018-06-09T22:56:01Z")

</div>

hi I am planning to integrate mautic in ecommerce script.  
would you please send me your sample code to me.

to gain some idea regarding it.
