1

I'm using Twilio API to validate phone number, the rest api to do so using command line would be:

curl -XGET "https://lookups.twilio.com/v1/PhoneNumbers/415-701-2311" -u "{AccountSid}:{AuthToken}"

Now I wonder how I can implement this http get request using angular 2? So far I have:

private url_verifyEmail: string = 'https://emailverifierapi.com/v2/'

constructor(private http: Http) {}
verifyPhone(phone: string): Observable<any> {
    let params = new URLSearchParams()
    params.set('AccountSid', `${constants.twilioAcountSID}`)
    params.set('AuthToken', `${constants.twilioAuthToken}`)

    return this.http.get(`${this.url_verifyPhone}${phone}`, {search: params})
                    .map(this.extractResponseData)
                    .catch(this.handleError)
}

But the response gives me unauthorized, saying that accountSid or authToken is incorrect? Any help would be appreciated. Thanks.

dulan
  • 1,584
  • 6
  • 22
  • 50

2 Answers2

2

This might work:

constructor(private http: Http) {}
verifyPhone(phone: string): Observable<any> {
    return this.http.get(`https://${constants.twilioAcountSID}:${constants.twilioAuthToken}@lookups.twilio.com/v1/PhoneNumbers/415-701-2311${phone}`)
                    .map(this.extractResponseData)
                    .catch(this.handleError)
}

(not supported in IE)

See also https://stackoverflow.com/a/38369923/217408

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

Twilio developer evangelist here.

We recommend that you do not make API requests from the client side of an application. To do so, you would have to either store or be able to access your Twilio account credentials (Account SID and Auth Token) from the client side.

This means that an attacker could steal your credentials and take over your Twilio account.

Our recommendation is to access the API from your server. Here is how you would verify numbers in Node.js using the Lookups API.

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88