英文:
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))
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论