def generate_voice_access_token(self, from_number: str, identity_postfix=None, ttl=60 * 60): """Generates a token required to make voice calls from the browser. """ # identity is used by twilio to identify the user uniqueness at browser(or any endpoints). identity = from_number if identity_postfix: identity = '_'.join( [identity, self.safe_identity(identity_postfix)]) # Create access token with credentials token = AccessToken(self.account_sid, self.api_key, self.api_secret, identity=identity, ttl=ttl) # Create a Voice grant and add to token voice_grant = VoiceGrant( outgoing_application_sid=self.application_sid, incoming_allow=True, # Allow incoming calls ) token.add_grant(voice_grant) return token.to_jwt()
def token(): client_name = request.values.get('client') platform = request.values.get('platform') account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID) api_key = os.environ.get("API_KEY", API_KEY) api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET) app_sid = os.environ.get("APP_SID", APP_SID) if platform == 'iosdev': push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID_IOS", PUSH_CREDENTIAL_SID_IOS_DEV) elif platform == 'iosprod': push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID_IOS", PUSH_CREDENTIAL_SID_IOS_PROD) else: push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID_ANDROID", PUSH_CREDENTIAL_SID_ANDROID) if client_name: IDENTITY =client_name grant = VoiceGrant( push_credential_sid=push_credential_sid, outgoing_application_sid=app_sid ) token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY) token.add_grant(grant) k = {'accessToken': str(token)} return json.dumps(k)
def amazon_token(country_code, worker_id): _, identity = get_caller_identity(country_code, worker_id) token = AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY, TWILIO_API_SECRET, identity=identity) grant = VoiceGrant(outgoing_application_sid=AMAZON_TWIML_APP_SID) token.add_grant(grant) return jsonify({"token": token.to_jwt()})
def token(self, request, pk=None): user = CustomUser.objects.get(pk=pk) token = AccessToken(account_sid, api_key, api_key_secret, identity='user' + str(user.id)) voice_grant = VoiceGrant(outgoing_application_sid=app_sid, push_credential_sid=push_credential_sid, incoming_allow=True) token.add_grant(voice_grant) return HttpResponse(token.to_jwt().decode('utf-8'), )
def test_programmable_voice_grant_incoming(self): grant = VoiceGrant(incoming_allow=True) scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.add_grant(grant) token = scat.to_jwt() assert_is_not_none(token) decoded_token = AccessToken.from_jwt(token, 'secret') self._validate_claims(decoded_token.payload) assert_equal(1, len(decoded_token.payload['grants'])) assert_equal({'incoming': { 'allow': True }}, decoded_token.payload['grants']['voice'])
def obtain_token(request): if request.method == "GET": voice_grant = VoiceGrant( outgoing_application_sid=settings.TWIML_APPLICATION_SID, incoming_allow=True, ) access_token = AccessToken( settings.TWILIO_ACCOUNT_SID, settings.API_KEY, settings.API_SECRET, ) access_token.add_grant(voice_grant) token = access_token.to_jwt() return JsonResponse({'token': token.decode('utf-8')})
def token(): account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID) api_key = os.environ.get("API_KEY", API_KEY) api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET) push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID) app_sid = os.environ.get("APP_SID", APP_SID) grant = VoiceGrant(push_credential_sid=push_credential_sid, outgoing_application_sid=app_sid) token = AccessToken(account_sid, api_key, api_key_secret, identity=IDENTITY) token.add_grant(grant) return str(token)
def token(): account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID) api_key = os.environ.get("API_KEY", API_KEY) api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET) push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID) app_sid = os.environ.get("APP_SID", APP_SID) grant = VoiceGrant(push_credential_sid=push_credential_sid, outgoing_application_sid=app_sid) identity = request.values["identity"] \ if request.values and request.values["identity"] else IDENTITY token = AccessToken(account_sid, api_key, api_key_secret, identity=identity) token.add_grant(grant) return token.to_jwt()
def get_token(request): identity = 'support_agent' if 'dashboard' in request.GET[ 'forPage'] else 'customer' # Create access token with credentials access_token = AccessToken(settings.TWILIO_ACCOUNT_SID, settings.API_KEY, settings.API_SECRET, identity=identity) # Create a Voice grant and add to token voice_grant = VoiceGrant( outgoing_application_sid=settings.TWIML_APPLICATION_SID, incoming_allow=True, # Optional: add to allow incoming calls ) access_token.add_grant(voice_grant) token = access_token.to_jwt() return JsonResponse({'token': token.decode('utf-8')})
def test_programmable_voice_grant(self): grant = VoiceGrant(outgoing_application_sid='AP123', outgoing_application_params={'foo': 'bar'}) scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.add_grant(grant) token = scat.to_jwt() assert_is_not_none(token) decoded_token = AccessToken.from_jwt(token, 'secret') self._validate_claims(decoded_token.payload) assert_equal(1, len(decoded_token.payload['grants'])) assert_equal( { 'outgoing': { 'application_sid': 'AP123', 'params': { 'foo': 'bar' } } }, decoded_token.payload['grants']['voice'])
def get_token(): identity = twilio_number outgoing_application_sid = twiml_app_sid access_token = AccessToken(account_sid, api_key, api_key_secret, identity=identity) voice_grant = VoiceGrant( outgoing_application_sid=outgoing_application_sid, incoming_allow=True, ) access_token.add_grant(voice_grant) response = jsonify({ 'token': access_token.to_jwt().decode(), 'identity': identity }) response.headers.add('Access-Control-Allow-Origin', '*') return response
def token(): account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID) api_key = os.environ.get("API_KEY", API_KEY) api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET) push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID) app_sid = os.environ.get("APP_SID", APP_SID) grant = VoiceGrant( push_credential_sid=push_credential_sid, outgoing_application_sid=app_sid ) # identity = request.values["identity"] \ # if request.values and request.values["identity"] else IDENTITY # Generate a random user name identity = alphanumeric_only.sub('', fake.user_name()) token = AccessToken(account_sid, api_key, api_key_secret, identity=identity) token.add_grant(grant) print("identity is "+identity) return jsonify(identity=identity, token=token.to_jwt())
import os from twilio.jwt.access_token import AccessToken from twilio.jwt.access_token.grants import VoiceGrant # required for all twilio access tokens # To set up environmental variables, see http://twil.io/secure account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_KEY_SECRET'] # required for Voice grant outgoing_application_sid = 'APxxxxxxxxxxxxx' identity = 'user' # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity=identity) # Create a Voice grant and add to token voice_grant = VoiceGrant( outgoing_application_sid=outgoing_application_sid, incoming_allow=True, # Optional: add to allow incoming calls ) token.add_grant(voice_grant) # Return token info as JSON print(token.to_jwt())
from twilio.jwt.access_token import AccessToken from twilio.jwt.access_token.grants import VoiceGrant # required for all twilio access tokens account_sid = 'ACxxxxxxxxxxxx' api_key = 'SKxxxxxxxxxxxx' api_secret = 'xxxxxxxxxxxxxx' # required for Voice grant outgoing_application_sid = 'APxxxxxxxxxxxxx' identity = 'user' # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity=identity) # Create a Voice grant and add to token voice_grant = VoiceGrant(outgoing_application_sid=outgoing_application_sid) token.add_grant(voice_grant) # Return token info as JSON print(token.to_jwt())