401 Unauthenticated Error [HELP]

Posted 6 months ago by theUnforgiven

Hi all,

Having a totally brain freeze here...I'm trying to post to the MessageBird API to send an SMS using Ajax.

$("#mob").on('focusout', function(e){

                    type: 'POST',
                    url: '/api/mobile/messagebird',
                    data: {'number': mob },
                    dataType: 'json',
                    success: function (data) {
                    error: function (data) {
                        console.log('An error occurred.');

And my controller looks like:

public function sendSMS(Request $request)
        try {
            $client = new \MessageBird\Client('key from env');
            $message = new \MessageBird\Objects\Message();
            $message->originator = 'MessageBird';
            $message->recipients = '44750000000';
            $message->body = 'This is a test message.';
            $MessageResult = $client->messages->create(json_encode($Message));
           /// var_dump($MessageResult);
            return response()->json([
                'success' => true, 
                'response' => json_encode($MessageResult)
        } catch (\MessageBird\Exceptions\AuthenticateException $e) {
            // That means that your accessKey is unknown
            echo 'wrong login';
        } catch (\MessageBird\Exceptions\BalanceException $e) {
            // That means that you are out of credits, so do something about it.
            echo 'no balance';
        } catch (\Exception $e) {
            echo $e->getMessage();

But I keep seeing "Unauthenticated." and I'm not sure why. Can someone kindly help me out please. By the way I have the CSRF except set also so this shouldn't interfere.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.