Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

API Calls working in local test environment but not when onlin

More
3 years 2 months ago - 3 years 2 months ago #245173 by sbgmedia
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 5.3.31+220815
Own server or LimeSurvey hosting: Own server
Survey theme/template:
==================
I am trying to connect to the Limesurvey API. I have enabled it and can access it in my local test environment, however, when i upload my application online and run it, I get an error :The remote server returned an error: (403) Forbidden.Following is my code:
Code:
 Imports System.IO Imports System.Net Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq Public Class _Default     Inherits System.Web.UI.Page     Public Class JsnRPCclient         Private id As Integer = 0         Public Property URL As String         Public Property Method As String         Public Property Parameters As JObject         Public Property Response1 As JsonRPCresponse         Public Sub New()             Parameters = New JObject()             Response1 = Nothing         End Sub         Public Sub New(ByVal URL As String)             Me.URL = URL             Parameters = New JObject()             Response1 = Nothing         End Sub         Public Function Post() As String             Dim jobject As JObject = New JObject()             jobject.Add(New JProperty("jsonrpc", "2.0"))             jobject.Add(New JProperty("id", System.Threading.Interlocked.Increment(id)))             jobject.Add(New JProperty("method", Method))             jobject.Add(New JProperty("params", Parameters))             Dim PostData As String = JsonConvert.SerializeObject(jobject)             Dim encoding As UTF8Encoding = New UTF8Encoding()             Dim bytes As Byte() = encoding.GetBytes(PostData)             Dim requestn As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)             requestn.Method = "POST"             requestn.ContentType = "application/json"             requestn.KeepAlive = True             requestn.ContentLength = bytes.Length             Dim writeStream As Stream = requestn.GetRequestStream()             writeStream.Write(bytes, 0, bytes.Length)             writeStream.Close()             Dim response As HttpWebResponse = CType(requestn.GetResponse(), HttpWebResponse)             Dim responseStream As Stream = response.GetResponseStream()             Dim readStream As StreamReader = New StreamReader(responseStream, encoding.UTF8)             Response1 = New JsonRPCresponse()             Response1 = JsonConvert.DeserializeObject(Of JsonRPCresponse)(readStream.ReadToEnd())             Response1.StatusCode = response.StatusCode             Return Response1.ToString()         End Function         Public Sub ClearParameters()             Me.Parameters = New JObject()         End Sub     End Class     Public Class JsonRPCresponse         Public Property id As Integer         Public Property result As Object         Public Property [error] As String         Public Property StatusCode As HttpStatusCode         Public Sub New()         End Sub         Public Overrides Function ToString() As String             Return "{""id"":" & id.ToString() & ",""result"":""" & result.ToString() & """,""error"":" & [error] & (If((String.IsNullOrEmpty([error])), "null", """" & [error] & """")) & "}"         End Function     End Class     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load     End Sub     Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click         'Configure SSL/TLS settings         ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls         ServicePointManager.ServerCertificateValidationCallback = Function(sender2, certificate, chain, sslPolicyErrors) True         Dim Baseurl As String = "https://www.dcsmanagement.com.au/evf/admin/remotecontrol/"         Dim client As JsnRPCclient = New JsnRPCclient(Baseurl)         client.Method = "get_session_key"         client.Parameters.Add("username", "admin")         client.Parameters.Add("password", "123456")         client.Post()         Dim SessionKey As String = client.Response1.result.ToString()         'Console.WriteLine(SessionKey.ToString)         TextBox1.Text += SessionKey         client.ClearParameters()         If (client.Response1.StatusCode = System.Net.HttpStatusCode.OK) Then             client.Method = "list_surveys"             client.Parameters.Add("sSessionKey", SessionKey)             client.Post()         End If         client.ClearParameters()         Dim content As String = client.Response1.result.ToString()         ' Read the response content as a string         ' Dim jsonObject As JObject = JObject.Parse(content)         Dim jsonArray As JArray = JArray.Parse(content)         Dim t, i, d As String         For Each item As JObject In jsonArray             t = item.SelectToken("surveyls_title").ToString             i = item.SelectToken("sid").ToString             d = ""             If t.Contains("<br>") Then                 Dim StringParts() As String = Split(t, "<br>", 3)                 d = StringParts(2).ToString                 t = t.Replace("<br>", " ")                 Literal1.Text += "<tr><td>" & i.ToString & "</td><td>" & t.ToString & "</td><td>" & d.ToString & "</td><tr>"             ElseIf Not t.Contains("<br>") Then                 d = "---"                 t = t.Replace("<br>", " ")                 Literal1.Text += "<tr><td>" & i.ToString & "</td><td>" & t.ToString & "</td><td>" & d.ToString & "</td><tr>"             End If         Next         TextBox1.Text += content     End Sub End Class


 
Last edit: 3 years 2 months ago by sbgmedia.

Please Log in to join the conversation.

More
3 years 2 months ago #245181 by holch

Your LimeSurvey version: Version 5.3.31+220815


First I would highly recommend an update to the latest LS5 which today would be LS 5.6.31+230718.

Your current version is from August 2022 and thus almost 12 months old and hasn't receive any updates, bug fixes and security patches since then.

Probably won't solve the described problem, but others. :-)

Now 403 means that the server doesn't allow this.

- What are your settings in Global Settings > Interfaces?
- What is the API URL you are using?

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.

Please Log in to join the conversation.

More
3 years 2 months ago - 3 years 2 months ago #245184 by sbgmedia
Hello,

The URL is  dcsmanagement.com.au/evf/index.php/admin/remotecontrol
I also tried  dcsmanagement.com.au/evf/admin/remotecontrol

JSON-RPC is enabled.
Publish API and Set Access... are both OFF.

Again, everything works fine when running the app from within Visual Studio 2022 but once the app is published and uploaded to my hosting service, I get this 403 error.
Last edit: 3 years 2 months ago by sbgmedia.

Please Log in to join the conversation.

More
3 years 2 months ago #245185 by sbgmedia
I think it might be an issue with my hosting service.

To confirm, I setup a free ASP.NET hosting account and uploaded the app and everything worked fine.

Please Log in to join the conversation.

More
3 years 2 months ago #245204 by sbgmedia
Hi all,

It has been sorted. it looks like it was an issue with the temp folder and .net framework.

Cheers,
K

Please Log in to join the conversation.

Moderators: tpartner, holch

Lime-years ahead

Online-surveys for every purse and purpose