duangy / aliyunsdkcore

阿里短信SDK
Apache License 2.0
26 stars 3 forks source link

报错SDK.InvalidRegionId Can not find endpoint to access的解决 #6

Closed ghost closed 6 years ago

ghost commented 6 years ago
params = "{\"code\":\"12345\",\"product\":\"云通信\"}"
print(send_sms(__business_id, "我的手机号", "云通信", "SMS_137885156", params))

# 返回
b'{"Message":"params must be [a-zA-Z0-9] for verification sms","RequestId":"8AC4C6F8-F483-4AFC-87BD-30490D3A5A21","Code":"isv.INVALID_PARAMETERS"}'

难道params 不能包含子母和数字之外的字符?

简单修改demo有时能正常发送,但有时候会报错::

Traceback (most recent call last):
  File "demo_sms_send.py", line 43, in <module>
    "我的手机号", "我的签名", "模板号", params))
  File "demo_sms_send.py", line 35, in send_sms
    smsResponse = acs_client.do_action_with_exception(smsRequest)
  File "我的路径/aliyunsdkcore/client.py", line 259, in do_action_with_exception
    status, headers, body = self.implementation_of_do_action(acs_request)
  File "我的路径/aliyunsdkcore/client.py", line 224, in implementation_of_do_action
    endpoint = self._resolve_endpoint(request)
  File "我的路径/aliyunsdkcore/client.py", line 174, in _resolve_endpoint
    return endpoint_resolver.resolve_endpoint(self.__region_id, request, self._location_service)
  File "我的路径/aliyunsdkcore/profile/endpoint/endpoint_resolver.py", line 36, in resolve_endpoint
    error_msg.get_msg('SDK_INVALID_REGION_ID'))
aliyunsdkcore.acs_exception.exceptions.ClientException: SDK.InvalidRegionId Can not find endpoint to access.

代码如下:

# -*- coding: utf-8 -*-
from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
from aliyunsdkcore.client import AcsClient
import uuid

def send_sms(ak, sk, phone_numbers, sign_name, template_code, template_param=None,
             REGION="cn-hangzhou"):
    smsRequest = SendSmsRequest.SendSmsRequest()
    # 申请的短信模板编码,必填
    smsRequest.set_TemplateCode(template_code)

    # 短信模板变量参数
    if template_param is not None:
        smsRequest.set_TemplateParam(template_param)

    # 设置业务请求流水号,必填。
    business_id = uuid.uuid1()
    smsRequest.set_OutId(business_id)

    # 短信签名
    smsRequest.set_SignName(sign_name)

    # 数据提交方式
    # smsRequest.set_method(MT.POST)

    # 数据提交格式
    # smsRequest.set_accept_format(FT.JSON)

    # 短信发送的号码列表,必填。
    smsRequest.set_PhoneNumbers(phone_numbers)

    # 调用短信发送接口,返回json
    acs_client = AcsClient(ak, sk, REGION)
    smsResponse = acs_client.do_action_with_exception(smsRequest)

    # TODO 业务处理

    return smsResponse

params = "{\"code\":\"456\"}"
print(send_sms("我的ak", "我的sk",
               "我的手机号", "我的签名", "模板号", params))
ghost commented 6 years ago

问题解决了,要补充下面的代码:

from aliyunsdkcore.profile import region_provider
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"
region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)