Skip to main content
Question

How to Use OAuth2 Authentication Method In .Net

  • May 22, 2025
  • 3 replies
  • 3 views

Forum|alt.badge.img

I had Install-Package Box.V2 

 but can't Able to use 

 

String authCode = await OAuth2Sample.GetAuthCode(config.AuthCodeUri, New Uri(config.RedirectUri));
await client.Auth.AuthenticateAsync(authCode);

 

OAuth2Sample Is Not Available.

 

Please help me.

3 replies

Forum|alt.badge.img

Hi ,

 

It's a little bit dated, but I have this .Net example that I prepared a while back that should give you info on how to set up a sample app.

 

- Jon


Forum|alt.badge.img

those example didn't work.

I explain:

var task = Task.Run(

async () =>
{
bool ret = false;
BoxConfig config = new BoxConfig(CLIENTID, CLIENTSECRET, new Uri("http://127.0.0.1"));
var session = new OAuthSession(APPTOKEN, "N/A", 3600, "bearer");
var client = new BoxClient(config, session);

using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
BoxFileRequest requestParams = new BoxFileRequest()
{
Name = Path.GetFileName(filePath),
Parent = new BoxRequestEntity() { Id = "0" }
};

BoxFile file = await client.FilesManager.UploadAsync(requestParams, fileStream);
}
ret = File.Exists(filePath);
return ret;

});
task.Wait();
return task.Result;

 

This is the code in c#

On task.Wait() it pops with error:

 

System.AggregateException
HResult=0x80131500
Messaggio=Si sono verificati uno o più errori.
Origine=mscorlib
Analisi dello stack:
in System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
in System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
in System.Threading.Tasks.Task.Wait()
in Cloud.Box.uploadFile(String filePath) in E:\Lirasoft\iCloud\Box.cs: riga 53
in TestCloud.Form1.button5_Click(Object sender, EventArgs e) in E:\Lirasoft\TestCloud\Form1.cs: riga 140
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ButtonBase.WndProc(Message& m)
in System.Windows.Forms.Button.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.Run(Form mainForm)
in TestCloud.Program.Main() in E:\Lirasoft\TestCloud\Program.cs: riga 19

Eccezione interna 1:
BoxSessionInvalidatedException: Generata eccezione di tipo 'Box.V2.Exceptions.BoxSessionInvalidatedException'.


Forum|alt.badge.img

in apptoken i put developer token that i read lives few second. How to have apptoken instead any time i want to make a call ?