阿里通信[短信验证码]

阿里通信-短信验证码

1、阿里云短信服务官方文档使用指引:

 https://help.aliyun.com/document_detail/59210.html

2、登录阿里通信

 1)登录链接:https://www.aliyun.com/product/sms

 2)可以使用淘宝账号登录

 3)登录成功后,进入到这个界面:https://dysms.console.aliyun.com/dysms.htm#/overview

  界面:

  

 3、阿里通信-短信验证码使用

1)获取 AccessKey 和 ACCESS_KEY_SECRET:

    

2)新建签名及模板

 签名及模板审核通过后即可以使用了。


4、python 发送短信验证码

1)下载阿里短信服务下的SDK(python):https://help.aliyun.com/document_detail/112147.html?spm=a2c4g.11186623.6.631.673f5f30ZSc6vT

 

在项目的虚拟环境中,执行下述命令,安装阿里云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并应用在我们自己的项目中

 

  使用 python SDK 的示例指导链接:https://help.aliyun.com/document_detail/53090.html?spm=a2c1g.8271268.10000.95.4048df25vZC2wH

  短信API demo 介绍:

  QuerySendDetails:调用QuerySendDetails接口查看短信发送记录和发送状态 , 文档链接:https://help.aliyun.com/document_detail/102352.html

  SendSms:发送短信 , 文档链接:https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.6.616.209b202aiMj6CQ

  SendBatchSms:批量发送短信, 文档链接:https://help.aliyun.com/document_detail/102364.html?spm=a2c4g.11186623.6.615.564256e0FVr3Zk

 3)SendSms 相关参数介绍

  请求参数:

  

  

  

 返回参数:

  

 返回结果 json 格式:

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

 错误码 error code:https://error-center.aliyun.com/status/product/Dysmsapi?spm=a2c4g.11186623.2.15.564256e0Lmgckj


 4)SendSms API 生成

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

  使用:

  

 API 调试生成 python 代码:

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', '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 aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

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']

 5)阿里云发送短信结合项目短信验证码使用

  代码链接参考(注册短信验证码):https://www.cnblogs.com/Eric15/articles/10681450.html


原文地址:https://www.cnblogs.com/Eric15/p/10925460.html