Meneghini
Meneghini

Reputation: 187

Box.com Get Request RestSharp C#

I have a problem where my POST request returns back a token but when I change my code to use the token and try a GET request, it gives me a "Status:0" message. Am I writing this code wrong? I've tried adding "Bearer " + token to the Authentication.

ErrorException = {"Cannot send a content-body with this verb-type."}

Post:

var client = new RestClient("https://api.box.com/oauth2/token"); RestRequest request = new RestRequest() { Method = Method.Post };

        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddParameter("client_id", $"{client_ID}");
        request.AddParameter("client_secret", $"{client_secret}");
        request.AddParameter("grant_type", "client_credentials");
        request.AddParameter("box_subject_type", "enterprise");
        request.AddParameter("box_subject_id", enterpriseID);

        var response = await client.ExecuteAsync(request);
        var responseMessage = JObject.Parse(response.Content);

GET:

            var client2 = new RestClient("https://api.box.com/2.0/files/154072314030");
            var request2 = new RestRequest() { Method = Method.Get };

            request2.AddHeader("Authorization",  token);
            request2.AddHeader("Content-Type", "application/json");

            var response2 = await client2.ExecuteAsync(request2);
            var responseMessage2 = JObject.Parse(response2.Content);

Upvotes: 1

Views: 506

Answers (1)

Meneghini
Meneghini

Reputation: 187

Ended up using their SDK. This is how I was able to login and pull the items in the folder:

BoxFolder folderWithLink = new BoxFolder(); BoxSharedLinkRequest linkRequest = new BoxSharedLinkRequest();

        int offset = 0;
        int count = 0;
        string folderID = "";

        //create connection to Box.com
        var boxConfig = new BoxConfigBuilder("CLIENT ID", "CLIENT SECRET", "Enterprise ID", "PRIVATE KEY", "PRIVATE KEY PASSWORD", "PUBLIC KEY ID").Build();
        var boxJWT = new BoxJWTAuth(boxConfig);
        var adminToken = await boxJWT.AdminTokenAsync();
        var client = boxJWT.AdminClient(adminToken);


        //Get the parent folder information and find the Batch folderID
        while (folderID == "")
        {
            var batches = await client.FoldersManager.GetFolderItemsAsync("FOLDER ID", 250, offset);
            foreach (var batchEntry in batches.Entries)
            {
                Console.Writeline(Batch.Name);
            }

Upvotes: 0

Related Questions