获取阿里钉钉TOKEN失败

使用webpack + iview + axios,在获取钉钉token是出现如下现象,响应里已经得到相关的JSON,但是ajax却提示ERROR,不知道问题是出在哪里,求解.

图片描述

AJAX代码如下:

query: function () {
                let url = 'https://oapi.dingtalk.com/gettoken';
                let CorpID = 'xxxx';
                let CorpSecret = 'xxxx';
                let accessTokenRequest = {
                    url: url,
                    method: 'get',
                    params: {
                        corpid: CorpID,
                        corpsecret: CorpSecret
                    },
                };
                axios(accessTokenRequest).then(response => {
                    console.info(response);
                }).catch(error => {
                    console.info(error);
                });
            },
阅读 6.2k
1 个回答
✓ 已被采纳新手上路,请多包涵

阿里钉钉不支持前端跨域访问,需要通过后端获取token后再返回前端。我采用的是.net作后端,官方没有提供SDK,需要自己写,需要使用到httpRequest类(我用的IDE是vs2017,在NuGet管理包里全称是FastHttpRequest),列出简单的代码如下

前端html

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="Scripts/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#getToken").click(function () {
                $.ajax({
                    type: "post",
                    url: "getToken.ashx",
                    success: function (data) {
                        console.info(data);
                    }, error: function () {
                        console.info("error");
                    }
                })
            });
        })
    </script>
</head>
<body>
    <button type="button" id ="getToken">获取token</button>
</body>
</html>

cs文件

using System;
using System.Collections.Generic;
using HttpRequest;

namespace myDDDev
{
    public static class DDConfig
    {
        public static string __CorpID = "xxxx";  //corpId
        public static string __CorpSecret = "xxxxx"; //corpSecret
    }
    public class M_AccessToken
    {
        public string access_token { get; set; }
        public int errcode { get; set; }
        public string errmsg { get; set; }
    }
    public static class DDHelper
    {
        public static string GetAccessToken(string url)
        {

            if (url != "")
            {
                try
                {
                    HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Get(url);
                    M_AccessToken oat = Newtonsoft.Json.JsonConvert.DeserializeObject<M_AccessToken>(result.ToStringResult());

                    if (oat != null)
                    {
                        if (oat.errcode == 0)
                        {
                            return oat.access_token;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return "";
        }
    }
}

ashx文件

using System;
using System.Web;
using myDDDev;
using System.Web.SessionState;

public class getToken : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        //获取AccessToken的方法是向 https://oapi.dingtalk.com/gettoken?corpid=id&corpsecret=secrect GET请求。
        string getUrl = string.Format("https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}", DDConfig.__CorpID, DDConfig.__CorpSecret);

        //access_token 会失效,需要定期获取;
        string access_token = DDHelper.GetAccessToken(getUrl);
        context.Session["token"] = access_token;
        context.Response.Write("access_token:" + access_token);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏