天天看點

阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)

1、 阿裡雲短信服務官方文檔使用指引

2、登入阿裡通信

 1)登入連結:

https://www.aliyun.com/product/sms

 2)可以使用淘寶賬号登入

 3)登入成功後,進入到這個界面:

https://dysms.console.aliyun.com/dysms.htm#/overview
阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)

3、阿裡通信-短信驗證碼使用

1)擷取 AccessKey 和 ACCESS_KEY_SECRET:

阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)

2)建立簽名及模闆

簽名及模闆稽核通過後即可以使用了。

4、python 發送短信驗證碼

1)下載下傳阿裡短信服務下的SDK(python):

https://help.aliyun.com/document_detail/112147.html?spm=a2c4g.11186623.6.631.673f5f30ZSc6vT
阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)

在項目的虛拟環境中,執行下述指令,安裝阿裡雲SDK核心庫:

python 2.x 安裝:

pip install aliyun-python-sdk-core

 python 3.x 安裝:

pip install aliyun-python-sdk-core-v3

2)使用 OpenAPI Explorer 來生成相關API的Demo并應用在我們自己的項目中

阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)
使用 python SDK 的示例指導連結: https://help.aliyun.com/document_detail/53090.html

短信API demo 介紹:

QuerySendDetails:調用QuerySendDetails接口檢視短信發送記錄和發送狀态 , 文檔連結:

https://help.aliyun.com/document_detail/102352.html SendSms:發送短信 , 文檔連結: https://help.aliyun.com/document_detail/101414.html SendBatchSms:批量發送短信, 文檔連結: https://help.aliyun.com/document_detail/102364.html

3)SendSms 相關參數介紹

請求參數:

阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)
傳回參數:
阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)

傳回結果 json 格式:

{

"Message":"OK",
"RequestId":"2184201F-BFB3-446B-B1F2-C746B7BF0657",
"BizId":"197703245997295588^0",
"Code":"OK"           

}

錯誤碼 error code:

https://error-center.aliyun.com/status/product/Dysmsapi

4)SendSms API 生成

進入OpenAPI Explorer :

https://api.aliyun.com/?spm=a2c4g.11186623.2.15.506959adEnxtsy#/?product=Dysmsapi&api=QuerySendDetails

使用:

阿裡通信[短信驗證碼]上線實戰流程分享!/usr/bin/env pythoncoding=utf-8python2: print(response)

API 調試生成 python 代碼:

!/usr/bin/env python

coding=utf-8

from aliyunsdkcore.client import AcsClient

from aliyunsdkcore.request import CommonRequest

client = AcsClient('', '', 'default')

request = CommonRequest()

request.set_accept_format('json')

request.set_domain('dysmsapi.aliyuncs.com')

request.set_method('POST')

request.set_protocol_type('https') # https | http

request.set_version('2017-05-25')

request.set_action_name('SendSms')

request.add_query_param('PhoneNumbers', "13*")

request.add_query_param('SignName', "小飯桌網站")

request.add_query_param('TemplateCode', "SMS_16")

request.add_query_param('TemplateParam', "{'code':'123456'}")

response = client.do_action(request)

python2: print(response)

print(str(response, encoding = 'utf-8'))

我們隻需要複制上述代碼,稍作改動,将code變成我們需要的驗證碼就可以了。

具體使用:

建立 random_code.py 檔案,代碼如下:

import random

def generate_code():

"""生成四位數的驗證碼"""
seeds = "1234567890"
random_str = []
for i in range(4):
    random_str.append(random.choice(seeds))
return "".join(random_str)           

建立 aliyunsdk.py 檔案,用于發送短信(可直接拿去使用),代碼如下:

import json

from utils.random_code import generate_code

def aliyun_send_sms():

"""阿裡雲發送短信"""
client = AcsClient('******', '***********', 'default')  # <accessKeyId> 、<accessSecret>

request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2017-05-25')
request.set_action_name('SendSms')

code = generate_code()   # 生成四位數的驗證碼

request.add_query_param('PhoneNumbers', "13*******")
request.add_query_param('SignName', "小飯桌網站")
request.add_query_param('TemplateCode', "SMS_********")
request.add_query_param('TemplateParam', json.dumps({'code': code}))  # 以json 格式傳遞code參數

response = client.do_action(request)  # 發送短信

response = str(response, encoding='utf-8')  # 傳回的response 為 <class 'bytes'> 類型,将其轉換成str字元串類型
response = json.loads(response)  # 再通過json.loads,将str類型的 response 轉換成dict 格式
# print(response)     # response:{'Message': 'OK', 'RequestId': 'F07A40C3-539C-453B-BE52-4B60FF8DF58E', 'BizId': '431121158876223912^0', 'Code': 'OK'}
# print(response['Code'])  # 擷取 Code 值
return code, response['Code']           

繼續閱讀