Problems with webrequests
Welcome to Portals United! / Forums / Troubleshooting / Problems with webrequests
- This topic has 13 replies, 3 voices, and was last updated 2 days, 14 hours ago by
cvona.
-
AuthorPosts
-
I am trying to request to authenticate to a server. To do this, I wrote the following node:
using System;
using System.Collections.Generic;
using WorldBuilder.Nodes;
using WorldBuilder.Nodes.IO;
using Worldbuilder.Nodes.FileIO;
using VocalChatbot;
using System.Threading.Tasks;
using ChatBotVocale_ConsoleApp;namespace MyNodes
{
public class MyAuthentication : NodeBase
{
private string Token;
public static string NODE_GUID => “B62F8E15-05ED-4400-884B-A97B3200DF7F”;
public static string NODE_CLASS_NAME => “Authentication”;public override Guid NodeClassGuid => Guid.Parse(NODE_GUID);
public override string NodeClassName => NODE_CLASS_NAME;public static List<NodeFactory.VarType> InputTypes => m_InputTypes;
public static List<NodeFactory.VarType> OutputTypes => m_OutputTypes;private static List<NodeFactory.VarType> m_InputTypes =
new List<NodeFactory.VarType> { NodeFactory.VarType.Flow,
};private static List<NodeFactory.VarType> m_OutputTypes =
new List<NodeFactory.VarType> { NodeFactory.VarType.Flow, NodeFactory.VarType.String,NodeFactory.VarType.Bool };private InputBase m_FlowInput;
private StringOutput m_TokenOutput;
private BoolOutput m_IsAuthenticatedOutput;public MyAuthentication(NodeTree nodeTree, Guid nodeInstanceGuid) :
base(nodeTree, nodeInstanceGuid, null, m_InputTypes, m_OutputTypes)
{
}public MyAuthentication(NodeTree nodeTree, NodeFile nodeFile, NodeFileEntry nodeFileEntry) :
base(nodeTree, nodeFileEntry.Identity, nodeFileEntry, m_InputTypes, m_OutputTypes)
{
}protected override void MakeIO(NodeFileEntry nodeFileEntry)
{
m_FlowInput = Inputs[0];
m_FlowInput.DisplayName = “Start”;
m_FlowInput.RunAction = () => Authentication();OutputBase flowOutput = Outputs[0];
flowOutput.DisplayName = “Next”;m_TokenOutput = Outputs[1] as StringOutput;
m_TokenOutput.DisplayName = “Access Token”;
m_TokenOutput.SetGetValueFunc(GetToken);
m_IsAuthenticatedOutput = Outputs[2] as BoolOutput;
m_IsAuthenticatedOutput.DisplayName = “Is Authenticated”;
m_IsAuthenticatedOutput.SetGetValueFunc(IsAuthenticated);
}private async void Authentication()
{
// Your logic here
Token = await VocalChatbotClass.ChatBotAuthentication();
// Continue execution flow
Run(); // This triggers the next node in the flow
}
private string GetToken()//changeName
{//Code here
return Token;}
private bool IsAuthenticated()//changeName
{//Code here
if (Token != “0”)
{ return true; }
return false;}
}
}where VocalChatbotClass.ChatBotAuthentication() is defined as “public static async Task<string> ChatBotAuthentication()”. I did many tests and my conclusion is that the PH doesn’t like the async when defining the method Authentication. I then tried to use the WebRequest node, but I wasn’t successful with that either. The output value wasn’t printing anything, the pop-ups constantly open up until the PH is quit. I wasn’t able to visualize long strings using the node “Display Text”. Basically, nothing I wanted to do worked out. Does someone have some suggestions or experiences with Web Request in the WB?
June 12, 2025 at 3:53 pm #1725I am not surprised that async function calls don’t work. But I see no reason why the WebRequest Node shouldn’t do what you want. We use the node in several other projects without any trouble.
I have quite some experience with web requests, so perhaps you’ll give me a chance and send me the authentication data (REST API link, json file with parameters filled out) and I’ll try to get the WebRequest node working for you. Deal? 😉
In my test, I did not include everything, since I wanted to test if it was working well or not. I was expecting an error message as output value, but this did not work. Should it work in your opinion? Why does async not work? It was very difficult to find the reason since F12 doesn’t work in the PH, and the files did not give errors during the upload.
I am not sure I can send you everything since the request is for authenticating on the server, maybe the best is if you send me an example working that I can try to replicate.
Thank you for your help.
I had a look at the WebRequest node script. It does not work because the node is not good for making an authentication.
I decided to modify it to work the way I want. What I should do is the following:
static async Task<string> Autentication()
{
string ret = “”;
string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($”{StringUtils.CLIENT_ID}:{StringUtils.CLIENT_SECRET}”));
string postData = “grant_type=client_credentials”;
Console.WriteLine($”credentials: {credentials}”);HttpWebRequest request = (HttpWebRequest)WebRequest.Create(StringUtils.ACCESS_TOKEN_URL);
request.Method = “POST”;
request.ContentType = “application/x-www-form-urlencoded”;
request.Headers[“Authorization”] = $”Basic {credentials}”;using (var stream = new StreamWriter(request.GetRequestStream()))
{
stream.Write(postData);
}
try
{
using (var response = (HttpWebResponse)request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
string result = reader.ReadToEnd();
Console.WriteLine(“Token Response: ” + result);// Estrai il token (usa un parser JSON se vuoi farlo in modo robusto)
var tokenStart = result.IndexOf(“access_token\”:\””) + 15;
var tokenEnd = result.IndexOf(“\””, tokenStart);
ret = result.Substring(tokenStart, tokenEnd – tokenStart);
}}
catch (WebException ex)
{
using var errorResponse = (HttpWebResponse)ex.Response;
using var reader = new StreamReader(errorResponse.GetResponseStream());
string errorText = await reader.ReadToEndAsync();
Console.WriteLine(“Errore server: ” + errorText);
ret = “0”;
}
return ret;}
By trying to modify the node, I also get some errors. The main one is [CS0012] WebRequestNode.cs(351,45): The type
‘WWWForm’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘UnityEngine.UnityWebRequestModule, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=null’, which, as you can see here, I also have it when uploading the WebRequestNode from the directory with all the examples. I tried to include “using UnityEngine.Networks;” but it does not solve the problem.June 13, 2025 at 9:24 am #1729a) to debug the Portal Hopper, you can either locate the player.log file which contains all the debug info of the last Hopper run. It is located at
C:\Users\<your user name>\AppData\LocalLow\Nuro\Portal Hopperb) I can’t send any working examples because we signed NDAs preventing that, but maybe we can compromise. You could send me an endpoint of your API and a json file with the values to send to the API and I can create a working example for you. It doesn’t even have to be the authentication. Any other endpoint would be OK. I would receive a “NOT AUTHORIZED” return error, which is a vaild response as well.
Or you could send the info to me via email to keep it hidden from the public eye to protect your company secrets.c) You may want to have a look at our implementation of the WebRequest node here This zip File contains all the nodes used by the World Builder/Portal Hopper. You may also want to have a look at the Json Node.
Hi Bob, I do not know if you saw my previous message. The problem of the WebRequestNode, is that inside is hardcoded headers[“Authorization”] = token.StartsWith( “Bearer ” ) ? token : “Bearer ” + token; while my header starts with “Basic”. I am trying to modify it, but I get this error when I add the script to the project:  [CS0012] WebRequestNode.cs(351,45): The type ‘WWWForm’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘UnityEngine.UnityWebRequestModule, Version=0.0.0.0,
June 13, 2025 at 2:04 pm #1733I think I have good news for you. Yes, I saw your message and we discussed this internally. We decided to provide a special Node which will have all the attributes of a webrequest node plus a microphone input which can record, send and stop. Once Record was turned on, the node will accumulate a byte stream from the mic. Then call send to send a web request with this recording as a file. Stop recording using the stop flow input. The node can be used to stream audio by calling send several times (i.e. 10) per second or as a audio file transfer node in teh way you wish to use it: record a command for the AI, stop recording and send.
And we will create an override text input so you can use “Basic” instead of “Bearer”.
Is this what you were looking for?
June 13, 2025 at 2:40 pm #1736We plan a release Wednesday next week (before the holiday season). I already started working on it. Fingers crossed that it’ll be ready then. If not, I’ll be back on Monday after the long weekend.
Hello Robert
I just have a quick.
I am working on to implement a node that can talk to GPT or any other openai. SO I can create an AI agent in WB. I will import an avatar to represent the agent and i want to talk to it with voice and with my voice command the node can send the request with prompt and get back to me with some answer.I read about the node you are working on in above post, Do you think it can help or solve my problem of AI agent as well?
June 18, 2025 at 2:04 pm #1779Yes, I think so. I just finished upgrading the WebRequest Node to make it more flexible. But maybe we should discuss this in a little more detail at the end of next Wednesday’s meeting. Our next Update is also delayed due to holidays season and another long weekend.
-
AuthorPosts
- You must be logged in to reply to this topic.