Specified access key is not found or invalid.阿里云发送短信遇到错误
使用idea控制台测试时遇到的问题,基于springBoot项目下(后面需要整合redis),下面附上自己开始代码
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKey ID ", "密码"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("PhoneNumbers", "132xxxxxxxxx");//手机号码 request.putQueryParameter("SignName", "方xxx自学平台");//签名名称 request.putQueryParameter("TemplateCode", "SMS_2xxxxxx");//签名模版CODE //生成6位随机数字 Integer number= (int)((Math.random()*9+1)*100000); Map<String,Object> map = new HashMap<String,Object>(); map.put("code",number); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));//验证码 try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } }
用的是Common模板,最开始以为是版本冲突,也以为自己的AccessKey权限问题,(后来发现权限并没有问题)于是改成了APIDemo的模板
// This file is auto-generated, don't edit it. Thanks. package com.aliyun.sample; import com.aliyun.tea.*; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSONObject; public class Sample { /** * 使用AK&SK初始化账号Client * @param accessKeyId * @param accessKeySecret * @return Client * @throws Exception */ public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception { com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() // 必填,您的 AccessKey ID .setAccessKeyId(accessKeyId) // 必填,您的 AccessKey Secret .setAccessKeySecret(accessKeySecret); // 访问的域名 config.endpoint = "dysmsapi.aliyuncs.com"; return new com.aliyun.dysmsapi20170525.Client(config); } /** * 使用STS鉴权方式初始化账号Client,推荐此方式。本示例默认使用AK&SK方式。 * @param accessKeyId * @param accessKeySecret * @param securityToken * @return Client * @throws Exception */ public static com.aliyun.dysmsapi20170525.Client createClientWithSTS(String accessKeyId, String accessKeySecret, String securityToken) throws Exception { com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() // 必填,您的 AccessKey ID .setAccessKeyId(accessKeyId) // 必填,您的 AccessKey Secret .setAccessKeySecret(accessKeySecret) // 必填,您的 Security Token .setSecurityToken(securityToken) // 必填,表明使用 STS 方式 .setType("sts"); // 访问的域名 config.endpoint = "dysmsapi.aliyuncs.com"; return new com.aliyun.dysmsapi20170525.Client(config); } public static void main(String[] args_) throws Exception { java.util.List<String> args = java.util.Arrays.asList(args_); // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html com.aliyun.dysmsapi20170525.Client client = Sample.createClient("AccessKey ID", "AccessKey Secret"); //生成6位随机数字 Integer number= (int)((Math.random()*9+1)*100000); Map<String,Object> map = new HashMap<String,Object>(); map.put("code",number); com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest() .setSignName("方xxx自学平台") .setTemplateCode("SMS_xxxxxxxx") .setPhoneNumbers("13xxxxxxx") .setTemplateParam(JSONObject.toJSONString(map)); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime); com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp)); } }
最后发现问题还是未解决!----------然后就去阿里云把完整代码下载下来运行进行测试,发现可以实现!---------可能自己环境搭建错了,但是很不理解自己也是搭建的完整项目!好在问题解决了!记录一下问题。