This C# program demonstrates how to retrieve a bearer token from the FedEx API and put it into an object, using credentials. If the credentials are good, the output should be similar to the output. Otherwise, the object will be empty.
This program requires installing the NuGet package Newtonsoft.Json in order to use the deserializing function to read the string into an object.
using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; // This requires the Newtonsoft NuGet package installation: "Newtonsoft.Json" using Newtonsoft.Json; namespace ReadFedExBearerTokenIntoObject { internal class Program { // This is a temporary object that is used for parsing the response public class CFedExToken { public CFedExToken() { expires_in = ""; access_token = ""; token_type = ""; scope = ""; } public string access_token { get; set; } public string expires_in { get; set; } public string token_type { get; set; } public string scope { get; set; } } static void Main(string[] args) { HttpClient client = new HttpClient(); List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>(); postData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials")); postData.Add(new KeyValuePair<string, string>("client_id", "l97962b486095e64be6801dab86aa55076")); postData.Add(new KeyValuePair<string, string>("client_secret", "af9ee3251569f0a3d4ec68e413e6a371")); // NOTE: This is the sandbox version of the api Task<HttpResponseMessage> sRequest = client.PostAsync("https://apis-sandbox.fedex.com/oauth/token", new FormUrlEncodedContent(postData)); Task<string> sResponse = sRequest.Result.Content.ReadAsStringAsync(); CFedExToken qTokenResp = JsonConvert.DeserializeObject<CFedExToken>(sResponse.Result); Console.WriteLine(JsonConvert.SerializeObject(qTokenResp, Formatting.Indented)); } } }
{ "access_token": "93obHmxs3MNv53Ar4pZFoxD28wjW6y+Y/DBBQkKY4Vu7tpoftEh77Yv79t31Snt7Pb89xu1jbk6KIq7Ux6YjvudwtbKnp/zTtbU7HV4vhHuIWxO3w9yauQ2SEsP+vwWTHyaEAnsiHuL0+R5Is1r/W3vtR8XF17MAdvLbHm593FzcvOH6EVaonHTuqIQ+tkLfamh48sXTpyFgk0ixdFB6EMQ0bkmkCLq2Twj0BZaKihOJoEWswPilMG5DY9nnRObHuQxMD4RdufHcG07+uF+EWfHM1W7utycmPMaVQirY3GVlH9ktlku1F6+pqfnuu11dtfz2ELfTFMIqxWCFdrEVgiuaIvrBROrmdorbcW6t3atXF+TY7Z/l91/gtpSCGXiEW4XX73+zaWjorVlVVUN0lrLhPOZCfnmR29XcdvMkvilt61YpNKPFacLwg5uDxthDYPxu4JcnSOHtQzwpHhgjuoV6HpKx5dy+Em8fjKNNh+Mwrtw/aFUca59M1B/bgscecfyJyvT6ujyeDfqLP5s9+xp+yRctzQifSLEprNBXDxy4f3V1dS0LH5JVED5YNlg5CByIeeLlBQsyPWVl5awM4BJDWDDR9Nq8hNvXbRbLKxemph53rllzzwvz52eQatkSAkIZVAgcAUuTLBZMQCTiYFVHuxpw1vnBrYMUBTGmk5Txm0eKIkU+JJXb+lOrVi3U0M4a5t53uH1PTPBY4Ic0CmUgAHKWLnAek9Wefq/Xpukz7NglJEoh+Cdv2/YXt98ftN69ODPzmqvz8uAOXcAtmzSZ53AZ68D3bIW29fT8ekpl5c/ZJYU7lSXGA6sGYYPQIU492l5aes0NBQWVPNFviBZhRmJKitX66JcnT96xZ+nST5Liso27YtAog4t1X2ElAMk4LN1l0ugScmeVH6QoBigID42hQtBY6lTt9W6Pp5QUBQEDYLjkyffeSYoChuL98Exp6VyKgud8H7ysH44B+qDpTG6TKc7Erh5OOZ2pol/tyk7YfhO5FuztjtIqjYEVgtBB+E64ysoeyE9KgiCnUhzAbvWCSzMyKioWLYKwjGpZLhKEUQbk8vvxzAWkZOkLKYQFjwHOCj+4neE2QGOsDATcw22l/uLe/n7gNJ/bhaQohSAe8vhw/SHNLStZZnasLy4uFjQwtNgaBTQWuTh4MeAvnpktnjkqODI4CK/jE6TIzGQxlrCym5CkooCAVeIJWpOkiV2FVXqHY9eONbm5jrcXLnyQL3/LsIPQsSmsUC8pcS8EEFny7qE1a+7mZ", "expires_in": "3599", "token_type": "bearer", "scope": "CXS-TP" } Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.