twilio / twilio-go

A Go package for communicating with the Twilio API.
MIT License
271 stars 40 forks source link

Authenticating with API key while using a custom client #222

Open kayex opened 5 months ago

kayex commented 5 months ago

Issue Summary

I'm trying to authenticate with an API key and secret and at the same time use a custom underlying HTTP client. I have read the instructions on how to authenticate with API Keys here, and the instructions on how to use a custom HTTP client here. However, I can't seem to make these two work together.

Below is what I've tried:

Code Snippet

import (
    "net/http"
    "github.com/twilio/twilio-go"
    "github.com/twilio/twilio-go/client"
)

httpClient := http.DefaultClient
apiKey := "REDACTED"
apiSecret := "REDACTED"
accountSID := "REDACTED"

credentials := &client.Credentials{
    Username: apiKey,
    Password: apiSecret,
}

twilioClient := &client.Client{
    Credentials: credentials,
    HTTPClient:  cl,
}
twilioClient.SetAccountSid(accountSID)

restClient := twilio.NewRestClientWithParams(twilio.ClientParams{
    Username:   apiKey,
    Password:   apiSecret,
    AccountSid: accountSID,
    Client:     twilioClient,
})

I'm then using the RestClient like this:

import (
    api "github.com/twilio/twilio-go/rest/api/v2010"
)

p := &api.CreateMessageParams{}
p.SetTo("+123456789")
p.SetFrom("+123456789")
p.SetBody("Test")

resp, err := httpClient.Api.CreateMessage(p)

I've tried omitting the Account SID as well as swapping the API Key and the Account SID.

Exception/Log

Status: 401 - ApiError 20003: Authenticate (null) More info: https://www.twilio.com/docs/errors/20003

Technical details:

tiwarishubham635 commented 1 month ago

Hi @kayex! I used the following code snippet and it worked for me:

import (
    "encoding/json"
    "fmt"
    "github.com/twilio/twilio-go"
    "github.com/twilio/twilio-go/client"
    api "github.com/twilio/twilio-go/rest/api/v2010"
    "net/http"
    "os"
)

func main() {
    httpClient := http.DefaultClient
    accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
    authToken := os.Getenv("TWILIO_AUTH_TOKEN")

    credentials := &client.Credentials{
        Username: accountSid,
        Password: authToken,
    }

    twilioClient := &client.Client{
        Credentials: credentials,
        HTTPClient:  httpClient,
    }

    twilioClient.SetAccountSid(accountSid)

    restClient := twilio.NewRestClientWithParams(twilio.ClientParams{
        Client: twilioClient,
    })

    twilioNumber := os.Getenv("TWILIO_PHONE_NUMBER")
    receiverNumber := os.Getenv("RECEIVER_PHONE_NUMBER")

    p := &api.CreateMessageParams{}
    p.SetTo(receiverNumber)
    p.SetFrom(twilioNumber)
    p.SetBody("Test Message")

    resp, err := restClient.Api.CreateMessage(p)
    if err != nil {
        fmt.Println(err.Error())
    } else {
        response, _ := json.Marshal(*resp)
        fmt.Println("Response: " + string(response))
    }
}

Let me know if you need any other help else we'll close this issue. Thanks!

kayex commented 1 month ago

Thank you for your response. I have managed to authenticate and send a message using the account SID and auth token. However, I do not understand how to use API keys to perform the authentication.

Is this not possible using the SDK?