Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburnette/3e681c4984f5751b6162 to your computer and use it in GitHub Desktop.
Save cburnette/3e681c4984f5751b6162 to your computer and use it in GitHub Desktop.
Call the Box API without using the Box .NET SDK (i.e. you only have VS 2010)
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
//install NuGet package 'RestSharp'
//install NuGet package Json.NET'
namespace Box_API_Rest_Example
{
class Program
{
const string DEVELOPER_TOKEN = "{YOUR_DEVELOPER_TOKEN}";
const string BASE_URL = "https://api.box.com/2.0";
//for this example program just click on a folder in Box and extract the folder ID from the URL
const string FOLDER_ID = "2764335289";
static void Main()
{
var client = new RestClient(BASE_URL);
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(DEVELOPER_TOKEN, "Bearer");
var request = new RestRequest("folders/{id}", Method.PUT);
request.AddUrlSegment("id", FOLDER_ID);
dynamic sharedLinkAttributes = new ExpandoObject();
sharedLinkAttributes.access = "open";
dynamic sharedLink = new ExpandoObject();
sharedLink.shared_link = sharedLinkAttributes;
request.AddJsonBody(sharedLink);
var response = client.Execute(request);
var content = response.Content;
dynamic parsed_content = JObject.Parse(content);
string shared_link_url = parsed_content.shared_link.url;
Console.WriteLine("Shared Link URL: {0}", shared_link_url);
Console.WriteLine("Press Return to exit...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment