Open kayex opened 5 months 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!
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
I'm then using the
RestClient
like this:I've tried omitting the Account SID as well as swapping the API Key and the Account SID.
Exception/Log
Technical details: