Skip to content

Instantly share code, notes, and snippets.

@allenmichael
Last active September 18, 2017 21:44
Show Gist options
  • Save allenmichael/bf9862c8bd89eb8726436853714835e6 to your computer and use it in GitHub Desktop.
Save allenmichael/bf9862c8bd89eb8726436853714835e6 to your computer and use it in GitHub Desktop.
Authenticate and get Service Account user info
using System;
using System.IO;
using System.Threading.Tasks;
using Box.V2;
using Box.V2.Config;
using Box.V2.JWTAuth;
namespace BoxPlayground
{
public class Program
{
static void Main(string[] args)
{
try
{
// Create an async method to execute and await Box SDK methods.
ExecuteMainAsync().Wait();
}
catch (Exception ex)
{
// Log any errors for debugging
Console.WriteLine(ex);
}
}
private static async Task ExecuteMainAsync()
{
// Open a stream to read and dispose of the automatically created Box configuration file.
using (FileStream fs = new FileStream($"./config.json", FileMode.Open))
{
// Initialize the SDK with the Box configuration file and create a client that uses the Service Account.
var session = new BoxJWTAuth(BoxConfig.CreateFromJsonFile(fs));
var serviceAccountClient = session.AdminClient(session.AdminToken());
// Use the GetCurrentUserInformationAsync method to retrieve current user's information.
// Since this client uses the Service Account, this will return the Service Account's information.
var serviceAccountUserInfo = await serviceAccountClient.UsersManager.GetCurrentUserInformationAsync();
// Log the Service Account's login value which should contain "AutomationUser".
// For example, [email protected]
Console.WriteLine(serviceAccountUserInfo.Login);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment