3

I am receiving the following error

[2018-12-18 12:12:46] local.ERROR: Credentials are required to create a Client {"exception":"[object] (Twilio\Exceptions\ConfigurationException(code: 0): Credentials are required to create a Client at C:\wamp64\www\_javid\javid\vendor\twilio\sdk\Twilio\Rest\Client.php:157)

I will include the code below and the source i used to create it. I would like to add, this was all working correctly the other evening.

Today, i merely added a new function to handle the saving of messages to the database. Then i started receiving the above error. Naturally i reverted my changes but still the same error.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


use App\User;
use Illuminate\Support\Facades\Auth;
use JWTAuth;
use App\Item;
use Log;

use Twilio\Rest\Client;

class MessagingController extends Controller
{
    protected $client;

    public function __construct(Client $client){
        $this->client = $client;
    }


    /**
     * Show the form for creating a notification.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('notifications.create');
    }


    public function sendMessage(request $request){
        $details = $request->only('membershipNumber', 'countryCode', 'message');

        $user = User::where('membership_number', $details['membershipNumber'])->with('mobile_number')->first();
        if(count($user)>0){
            $this->messageSaveToDatabase($details, $user);
            $this->messageSendToMobile($details, $user);
            $this->messageSendToEmail($details, $user);
            return response([
                'status' => 'success',
                'msg' => __('messages.success'),
                'response' => $details
               ], 200);
        } else {
            return response([
                'status' => 'error',
                'msg' => __('messages.error')
               ], 200);
        }

    }

    protected function messageSaveToDatabase($details, $user){

    }
    protected function messageSendToMobile($details, $user, $imageUrl = null){
        $lineBreak = "\n\n";
        $phoneNumber = $user->mobile_number->country_code.decrypt($user->mobile_number->number);

        $message = "Hi member #".$details['membershipNumber'].$lineBreak.
            $details['message'];

        $twilioPhoneNumber = config('services.twilio')['phoneNumber'];
        $messageParams = array(
            'from' => $twilioPhoneNumber,
            'body' => $message
        );
        if ($imageUrl) {
            $messageParams['mediaUrl'] = $imageUrl;
        }

        $this->client->messages->create(
            $phoneNumber,
            $messageParams
        );
    }
    protected function messageSendToEmail($details, $user){

    }
}

I have checked the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN, these are both correct.

The code was taken from the following guide, i stripped out the subscriber part. Guide from Twilio

one more thing, I found the following Here which suggests i need to do something like this $client = new Client($keySid, $keySecret, $accountSid); but the guide above, does not do this, plus it all worked like this also.

Any help or suggestions would be great, i'm running out of hair to pull out :(

The-WebGuy
  • 877
  • 5
  • 12
  • 25

1 Answers1

4

After a little more googling and some re-working, I found a working solution

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;
use Illuminate\Support\Facades\Auth;


use Twilio\Rest\Client;

class MessagingController extends Controller
{

    protected function messageSendToMobile($details, $message, $user, $imageUrl = null){
        $accountSid = env('TWILIO_ACCOUNT_SID');
        $authToken = env('TWILIO_AUTH_TOKEN');
        $twilioNumber = env('TWILIO_PHONE_NUMBER');
        $lineBreak = "\n\n";
        $to = $user->mobile_number->country_code.decrypt($user->mobile_number->number);
        $client = new Client($accountSid, $authToken);
        try {
            $client->messages->create(
                $to,
                [
                    "body" => $message,
                    "from" => $twilioNumber
                ]
            );
            Log::info('Message sent to ' . $twilioNumber);
        } catch (TwilioException $e) {
            Log::error(
                'Could not send SMS notification.' .
                ' Twilio replied with: ' . $e
            );
        }
    }
}
The-WebGuy
  • 877
  • 5
  • 12
  • 25
  • 2
    Can you please mention the cause? Since sometimes finding out the difference between two code snippet is not suitable choice. Thanks – Mushfiqur Rahman Jan 20 '19 at 12:25
  • This was a while ago, but, I believe it is the way Client is used. not this: `$this->client = $client;` but this: `$client = new Client($accountSid, $authToken);` – The-WebGuy Jan 22 '19 at 07:37
  • Thanks for your reply. It means at your first code you missed the `$accountSid, $authToken` values when you were creating the `new Client` object, right? – Mushfiqur Rahman Jan 22 '19 at 09:48
  • Yes, but most importantly, how i'd missed the creating of the new client object `$client = new Client()` – The-WebGuy Jan 24 '19 at 08:03
  • 1
    in the OP - see `protected $client;` and `$this->client = $client;` which is wrong, i believe this was correct at one-time, but it is possible that they updated their API or the Laravel package – The-WebGuy Jan 24 '19 at 08:05