鍍金池/ 問答/Java  HTML/ 獲取阿里釘釘TOKEN失敗

獲取阿里釘釘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);
                });
            },
回答
編輯回答
不討喜

阿里釘釘不支持前端跨域訪問,需要通過后端獲取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;
        }
    }

}
2017年12月2日 12:16