Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticating with API key while using a custom client #222

Open
kayex opened this issue Jan 26, 2024 · 2 comments
Open

Authenticating with API key while using a custom client #222

kayex opened this issue Jan 26, 2024 · 2 comments

Comments

@kayex
Copy link

kayex commented Jan 26, 2024

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:

  • twilio-go version: v1.17.0
  • go version: 1.21.1
@tiwarishubham635
Copy link
Contributor

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
Copy link
Author

kayex commented Jun 3, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants