Skip to content

Adding Code Snippets #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 3, 2020
29 changes: 29 additions & 0 deletions secrets/create-secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import nexmo, os, getpass
from os.path import join, dirname
from dotenv import load_dotenv

#Load the environment
envpath = join(dirname(__file__),'./.env')
load_dotenv(envpath)

#Init the client
client = nexmo.Client(
key = os.getenv('NEXMO_API_KEY'),
secret = os.getenv('NEXMO_API_SECRET')
)

#Get data from user from keyword
api_key = input("Enter your api_key: ")
#Use getpass instead of input to mask secret
new_api_secret = getpass.getpass("Enter secret: ")

#Create the secret
try:
response = client.create_secret(api_key, new_api_secret)
if "id" in response:
print("Key was created\nId: {}\nCreated at: {}\nLinks: {}".format(response["id"], response["created_at"], response["_links"]["self"]["href"]))
except:
print(
"Error: Secret does not meet complexity requirements. Please check the link below for more details:\n",
"https://developer.nexmo.com/api-errors/account/secret-management#validation"
)
33 changes: 33 additions & 0 deletions sms/send-signed-sms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#Import dependencies
import nexmo, os
from os.path import join, dirname
from dotenv import load_dotenv

#Load the environment
envpath = join(dirname(__file__),'./.env')
load_dotenv(envpath)

#Init the client
client = nexmo.Client(
key = os.getenv('NEXMO_API_KEY'),
secret = os.getenv('NEXMO_API_SECRET'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They shouldn't need to add an API Secret if they're signing (the signature acts as the auth)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took after the other code snippet models. My bad. Cleaning up

signature_secret = os.getenv('NEXMO_SIGNATURE_SECRET'),
signature_method = 'md5'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a difference between md5 hash and HMAC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, HMAC needs a cryptographic function like md5, sha1 to make the calculations... In other words HMAC depends on a cryptographic functions and other variables like a secret. Is there a specific requirement for either? i just went with md5 as the standard.

)

#Define variables - replace FROM_NUMBER and TO_NUMBER with actual numbers
from_number = FROM_NUMBER
to_number = TO_NUMBER
text = 'A text message sent using the Nexmo SMS API'

#Sending the sms
response = client.send_message({
"from": from_number,
"to": to_number,
"text": text
})

if response["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {response['messages'][0]['error-text']}")