使用twilio-go客户端库是否可以指定出站代理?

huangapple go评论85阅读模式
英文:

Is it possible to specify an outbound proxy using the twilio-go client library?

问题

我正在尝试使用twilio-go发送电话和短信,但运行客户端的主机必须通过出站代理才能访问互联网。是否有办法指定一个代理供twilio客户端使用?

英文:

I am trying to use twilio-go to send calls and sms messages but the host running the client has to go through an outbound proxy to reach the internet. Is there a way to specify a proxy for the twilio client to use?

答案1

得分: 1

看起来你可以构建一个使用*http.Client的twilio-go Client,你可以设置该Client使用代理。

更新:

我做了一个小例子来展示如何使用代理:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"

	"github.com/twilio/twilio-go/client"
	apiv2010 "github.com/twilio/twilio-go/rest/api/v2010"
)

func main() {
	from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
	to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
	body := os.Getenv("TWILIO_MSG_BODY")

	accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
	authToken := os.Getenv("TWILIO_AUTH_TOKEN")

	proxyUrl, err := url.Parse("http://localhost:8080")
	if err != nil {
		log.Panic(err)
	}

	c := client.Client{
		Credentials: &client.Credentials{
			Username: accountSid,
			Password: authToken,
		},
		HTTPClient: &http.Client{
			Transport: &http.Transport{
				Proxy: http.ProxyURL(proxyUrl),
			},
		},
	}
	c.SetAccountSid(accountSid)

	params := &apiv2010.CreateMessageParams{
		To:   &to,
		From: &from,
		Body: &body,
	}

	textMsgSvc := apiv2010.NewApiServiceWithClient(&c)
	resp, err := textMsgSvc.CreateMessage(params)
	if err != nil {
		log.Panic(err)
	} else {
		response, _ := json.Marshal(*resp)
		fmt.Println("Response: " + string(response))
	}
}

希望对你有帮助!

英文:

It seems like you can construct your twilio-go Client, which consumes *http.Client, which you can set to use the proxy.

UPD:

Made a little example to show how that would work:

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"github.com/twilio/twilio-go/client"
apiv2010 "github.com/twilio/twilio-go/rest/api/v2010"
)
func main() {
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
body := os.Getenv("TWILIO_MSG_BODY")
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
proxyUrl, err := url.Parse("http://localhost:8080")
if err != nil {
log.Panic(err)
}
c := client.Client{
Credentials: &client.Credentials{
Username: accountSid,
Password: authToken,
},
HTTPClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
},
},
}
c.SetAccountSid(accountSid)
params := &apiv2010.CreateMessageParams{
To:   &to,
From: &from,
Body: &body,
}
textMsgSvc := apiv2010.NewApiServiceWithClient(&c)
resp, err := textMsgSvc.CreateMessage(params)
if err != nil {
log.Panic(err)
} else {
response, _ := json.Marshal(*resp)
fmt.Println("Response: " + string(response))
}
}

huangapple
  • 本文由 发表于 2021年10月27日 07:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69730972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定