def token(request): device_id = request.GET.get('device', 'unknown') identity = request.GET.get('identity', 'guest').encode('utf-8') endpoint_id = "NeighborChat:{0}, {1}".format(device_id, identity) token = AccessToken(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_API_KEY, settings.TWILIO_API_SECRET, identity) grant = IpMessagingGrant() grant.service_sid = settings.TWILIO_IPM_SERVICE_SID grant.endpoint_id = endpoint_id token.add_grant(grant) response = {'identity': identity, 'token': token.to_jwt()} return JsonResponse(response)
def token(): if not session.get('logged_in'): return redirect(url_for('login')) else: # get credentials for environment variables account_sid = session.get('sid') print(session.get('sid')) api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] service_sid = os.environ['TWILIO_IPM_SERVICE_SID'] # create a randomly generated username for the client db = get_db() username = session.get('user') identity = username # Create a unique endpoint ID for the device_id = request.args.get('device') endpoint = "TwilioChatDemo:{0}:{1}".format(identity, device_id) # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=service_sid) token.add_grant(ipm_grant) # Return token info as JSON return jsonify(identity=identity, token=token.to_jwt())
def generateTwilioAccessToken(sender, **kwargs): print "user_logged_in receiver" print sender print kwargs['request'] user = kwargs['user'] try: doctor = user.doctor # create a randomly generated username for the client identity = user.username # <unique app>:<user>:<device> endpoint = "PalliAssist:" + identity + ":web" # Create access token with credentials token = AccessToken(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_API_KEY, settings.TWILIO_API_SECRET, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant( endpoint_id=endpoint, service_sid=settings.TWILIO_IPM_SERVICE_SID) token.add_grant(ipm_grant) doctor.twilio_token = token doctor.save() print doctor.twilio_token except Doctor.DoesNotExist: print "error. not a doctor logging in?"
def test_grants(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.add_grant(ConversationsGrant()) scat.add_grant(IpMessagingGrant()) token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal(2, len(payload['grants'])) assert_equal({}, payload['grants']['rtc']) assert_equal({}, payload['grants']['ip_messaging'])
def test_ip_messaging_grant(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.add_grant(IpMessagingGrant(service_sid='IS123', push_credential_sid='CR123')) token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal(1, len(payload['grants'])) assert_equal({ 'service_sid': 'IS123', 'push_credential_sid': 'CR123' }, payload['grants']['ip_messaging'])
def token(): identity = request.args.get('identity') device_id = request.args.get('device') endpoint = "TwilioChatDemo:{0}:{1}".format(identity, device_id) token = AccessToken( constants.TWILIO_ACCOUNT_SID, constants.TWILIO_API_KEY, constants.TWILIO_API_SECRET, identity) ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=constants.TWILIO_SERVICE_SID) token.add_grant(ipm_grant) return jsonify(identity=identity, token=token.to_jwt())
def tokens(request): identity = request.user['sub'] endpoint_id = request.GET["endpoint_id"] print("identity: {}\nendpoint: {}".format(identity, endpoint_id)) account_sid = "ACd3a83bf4c0646baaba77cdc355567d1f" api_key = "SK4949cc25487c7bd8001f3e0f21165064" api_secret = "keQ3hukqsJ8PP9WNLs2g6Fels5umPCzM" token = AccessToken(account_sid, api_key, api_secret, identity, ttl=40000) ipm_service_sid = "IS3ba6938016464f7cbcba3d2fea297dca" endpoint_id = ipm_service_sid + identity + endpoint_id push_credential_sid = "CRe9c5eff29e744709d7df875f8a797bf0" ipm_grant = IpMessagingGrant(endpoint_id=endpoint_id, service_sid=ipm_service_sid, push_credential_sid=push_credential_sid) token.add_grant(ipm_grant) return HttpResponse(json.dumps({ 'identity': identity, 'token': token.to_jwt() }))
def token(): # get credentials for environment variables account_sid = "ACcdbab0f13e08eb8b19b6d3025a9ad6f7" api_key = "SK2f52e17a9ca74d4714d28a7c575e1e21" api_secret = "6XYHaD6O5zPKDpM4wU34NknCQj7L1d6C" service_sid = "IS27b6d9077d6c48838881fc41b4748bb2" # create a randomly generated username for the client identity = fake.user_name() # Create a unique endpoint ID for the device_id = request.args.get('device') endpoint = "TwilioChatDemo:{0}:{1}".format(identity, device_id) # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=service_sid) token.add_grant(ipm_grant) # Return token info as JSON return jsonify(identity=identity, token=token.to_jwt())
def token(): # get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] service_sid = os.environ['TWILIO_IPM_SERVICE_SID'] # create a randomly generated username for the client identity = os.environ['TWILIO_USERNAME'] # Create a unique endpoint ID for the device_id = request.args.get('device') endpoint = "TwilioChatDemo:{0}:{1}".format(identity, device_id) # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=service_sid) token.add_grant(ipm_grant) # Return token info as JSON return jsonify(identity=identity, token=token.to_jwt())
from twilio.access_token import AccessToken, IpMessagingGrant # required for all twilio access tokens account_sid = 'ACxxxxxxxxxxxx' api_key = 'SKxxxxxxxxxxxx' api_secret = 'xxxxxxxxxxxxxx' # required for IP messaging grants ipm_service_sid = 'ISxxxxxxxxxxxx' identity = '*****@*****.**' device_id = 'someiosdevice' endpoint_id = "HipFlowSlackDockRC:{0}:{1}".format(identity, device_id) # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant(endpoint_id=endpoint_id, service_sid=ipm_service_sid) token.add_grant(ipm_grant) # Return token info as JSON print(token.to_jwt())
from twilio.access_token import AccessToken, IpMessagingGrant # required for all twilio access tokens account_sid = 'ACxxxxxxxxxxxx' api_key = 'SKxxxxxxxxxxxx' api_secret = 'xxxxxxxxxxxxxx' # required for IP messaging grants ipm_service_sid = 'ISxxxxxxxxxxxx' identity = '*****@*****.**' # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant(service_sid=ipm_service_sid) token.add_grant(ipm_grant) # Return token info as JSON print(token.to_jwt())