示例#1
0
def generate_text(number, message):
    # create an instance of the API class
    api_instance = Telstra_Messaging.AuthenticationApi()
    client_id = "Agl2rsjQ0fbLC1xqPGDNve2Oianci7wK"  # str |
    client_secret = "eWJoCzsYcTk2ITRl"  # str |
    grant_type = 'client_credentials'  # str |  (default to client_credentials)

    try:
        # Generate OAuth2 token
        api_response = api_instance.auth_token(client_id, client_secret,
                                               grant_type)
        access_token = api_response.__getattribute__('access_token')

        configuration = Telstra_Messaging.Configuration()
        configuration.access_token = access_token

        api_instance = Telstra_Messaging.MessagingApi(
            Telstra_Messaging.ApiClient(configuration))
        payload = {"to": number, "validity": "60", "body": message}

        try:
            # Send SMS
            api_response = api_instance.send_sms(payload)
        except ApiException as e:
            print("Exception when calling MessagingApi->send_sms: %s\n" % e)

    except ApiException as e:
        print("Exception when calling AuthenticationApi->auth_token: %s\n" % e)

    return "text sent"
示例#2
0
def get_token():
    access_token = r.get(TELSTRA_SMS_ACCESS_TOKEN)
    if access_token:
        return access_token

    configuration = Telstra_Messaging.Configuration()
    api_instance = Telstra_Messaging.AuthenticationApi(
        Telstra_Messaging.ApiClient(configuration))
    client_id = settings.TELSTRA_CLIENT_KEY
    client_secret = settings.TELSTRA_CLIENT_SECRET
    grant_type = 'client_credentials'

    try:
        # Generate OAuth2 token
        api_response = api_instance.auth_token(client_id, client_secret,
                                               grant_type)
        access_token = api_response.access_token
        expires_in = int(api_response.expires_in
                         ) if api_response.expires_in.isdigit() else 3599
        r.setex(TELSTRA_SMS_ACCESS_TOKEN, expires_in, access_token)
        return access_token
    except ApiException as e:
        log.error(
            "Exception when calling AuthenticationApi->auth_token: %s\n" % e)
        return None
示例#3
0
def get_token():
    global TELSTRA_ACCESS_TOKEN, TELSTRA_ACCESS_TOKEN_EXPIRY
    access_token = TELSTRA_ACCESS_TOKEN if TELSTRA_ACCESS_TOKEN_EXPIRY > datetime.now(
    ) else None
    if access_token:
        return access_token

    configuration = Telstra_Messaging.Configuration()
    api_instance = Telstra_Messaging.AuthenticationApi(
        Telstra_Messaging.ApiClient(configuration))
    client_id = configs.TELSTRA_CLIENT_KEY
    client_secret = configs.TELSTRA_CLIENT_SECRET
    grant_type = 'client_credentials'

    try:
        # Generate OAuth2 token
        api_response = api_instance.auth_token(client_id, client_secret,
                                               grant_type)
        access_token = api_response.access_token
        expires_in = int(api_response.expires_in
                         ) if api_response.expires_in.isdigit() else 3599
        TELSTRA_ACCESS_TOKEN = access_token
        TELSTRA_ACCESS_TOKEN_EXPIRY = datetime.now() + timedelta(
            seconds=expires_in)
        return access_token
    except ApiException as e:
        log.error(
            "Exception when calling AuthenticationApi->auth_token: %s\n" % e)
        return None
示例#4
0
    def auth(self):
        grant_type = 'client_credentials'
        api_instance = telstra.AuthenticationApi()

        resp = api_instance.auth_token(self._client_id, self._client_secret, grant_type)
        conf = telstra.Configuration()
        conf.access_token = resp.access_token
        return conf
示例#5
0
    def authenticate_client(self):
        api_instance = Telstra_Messaging.AuthenticationApi(
            Telstra_Messaging.ApiClient(self.configuration))

        try:
            # Generate OAuth2 token
            self.api_response = api_instance.auth_token(
                self.client_id, self.client_secret, self.grant_type)

        except ApiException as e:
            print(
                "Exception when calling AuthenticationApi->auth_token: %s\n" %
                e)
示例#6
0
    def generateAuthToken(self):
        'Uses the Telstra messaging API client credientals to get an OAuth 2.0 token.'

        api_instance_Auth = Telstra_Messaging.AuthenticationApi()
        try:
            self.authToken = api_instance_Auth.auth_token(
                self.client_id, self.client_secret, self.grant_type)
            self.authTokenExpiry = datetime.now() + timedelta(seconds=3598)
            pprint(self.authToken)
        except ApiException as e:
            print(
                "Exception when calling AuthenticationApi->auth_token: %s\n" %
                e)

        self.configuration = Telstra_Messaging.Configuration()
        self.configuration.access_token = self.authToken.access_token
        self.provisionInstance = Telstra_Messaging.ProvisioningApi(
            Telstra_Messaging.ApiClient(self.configuration))
示例#7
0
    def auth(self):
        grant_type = 'client_credentials'

        api_instance = Telstra_Messaging.AuthenticationApi()

        start_time = monotonic()
        try:
            resp = api_instance.auth_token(self._client_id, self._client_secret, grant_type)

            self.logger.info("Telstra auth request succeeded")

            conf = Telstra_Messaging.Configuration()
            conf.access_token = resp.access_token

            return conf
        except Exception as e:
            self.logger.error("Telstra auth request failed")
            raise e
        finally:
            elapsed_time = monotonic() - start_time
            self.logger.info("Telstra auth request finished in {}".format(elapsed_time))
示例#8
0
from __future__ import print_function
import time
import Telstra_Messaging
from Telstra_Messaging.rest import ApiException
from pprint import pprint

# Defining host is optional and default to https://tapi.telstra.com/v2
api_instance = Telstra_Messaging.AuthenticationApi(
    Telstra_Messaging.ApiClient())
client_id = 'Mr0NZxs2KsXcbqXJOgX1PRMtP4zkkgwG'  # str |
client_secret = 'qy7JjW8g98397oAM'  # str |
grant_type = 'client_credentials'  # str |  (default to 'client_credentials')
scope = 'scope_example'  # str | NSMS (optional)

try:
    # Generate OAuth2 token
    api_response = api_instance.auth_token(client_id,
                                           client_secret,
                                           grant_type,
                                           scope=scope)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling AuthenticationApi->auth_token: %s\n" % e)