0

我尝试连接到以下 API https://api.ssg-wsg.sg/grantCalculators/individual。我收到以下错误“禁止访问此 API”。这些技能未来 API 是否存在任何 UAT?谢谢!

我的订阅 [1]:https ://i.stack.imgur.com/5ZgiQ.jpg

我的代码尝试{

            if (Session["access_token"]==null)
            {
                txtResult.Text = "Generate Token first.";
                    return;
            }                

            ServicePointManager.Expect100Continue = true;
            ServicePointManager.DefaultConnectionLimit = 9999;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
           // string url = "https://mock-api.ssg-wsg.sg/grantCalculators/individual";
            string url = "https://public-api.ssg-wsg.sg/grantCalculators/individual";
            // string url = "https://api.ssg-wsg.sg/grantCalculators/individual";
            // string data = "{\"trainingPartnerUen\":\"198201025C11\",\"courseReferenceNumber\":\"TGS-0026008-ES\"}";
            string data = "{\"trainingPartnerUen\":\"" + txtUEN.Text + "\",\"courseReferenceNumber\":\"";
            data+=txtReferenceNumber.Text+ "\",\"}";
            //url = url + data;
            WebRequest myReq = WebRequest.Create(url);              
            myReq.Method = "POST";
            string accesTocken = Session["access_token"].ToString();
            myReq.ContentType =  "application/json";
            using (var streamWriter = new StreamWriter(myReq.GetRequestStream()))
            { 
                streamWriter.Write(data);
            }
           
            myReq.Headers.Add("Authorization", "Bearer  " + accesTocken);
            var httpResponse = (HttpWebResponse)myReq.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                txtResult.Text=result;
            }             
           


            
        }
        catch (WebException webex)
        {
            WebResponse errResp = webex.Response;
            using (Stream respStream = errResp.GetResponseStream())
            {
                StreamReader reader = new StreamReader(respStream);
                string text = reader.ReadToEnd();
                txtResult.Text = text;
            }
        }
        catch (Exception ex)
        {
            txtResult.Text = ex.Message;
        }
4

2 回答 2

0

听起来您还没有订阅 API。我刚刚检查了这个 API 不需要任何批准,所以你可能希望订阅这个 API 并直接测试它。

API 招摇:https ://developer.ssg-wsg.sg/webapp/docs/product/7KU1xrpxljJZnsIkJP6QNF/group/IUAJ3XXeGEZuk9yGpoUX3/api/JSUVkOlAbD5KS2tpCw9aB/version/pgJTm66XDOVr61ZpCVFf

应用开发指南:https ://developer.ssg-wsg.sg/webapp/guides/6gvz7gEnwU2dSIKPrTcXnq

其他入门指南:https ://developer.ssg-wsg.sg/webapp/guides

于 2020-12-24T05:58:14.413 回答
0

我尝试运行您的代码,发现问题出在以下行,其中有一个双空格,之后Bearer导致无法识别访问令牌。

myReq.Headers.Add("Authorization", "Bearer  " + accesTocken);

解决方案:

myReq.Headers.Add("Authorization", "Bearer " + accesTocken);
于 2021-01-02T12:12:34.443 回答