-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Using the API with Csharp
RayBB edited this page May 17, 2024
·
1 revision
``# C# API examples
For API's that reference a specific users data such as those API's that start with people. Substitute {Profile Display Name} with the authenticated user or public data's profile display name.
https://openlibrary.org/people/{Profile Display Name}/books/want-to-read.json https://openlibrary.org/people/{Profile Display Name}/books/currently-reading.json https://openlibrary.org/people/{Profile Display Name}/books/already-read.json
public async Task<ErrorReturn> Login(string AccessKeyString, string SecretString, string OL_ProfileID)
{
_AccessKeyString = AccessKeyString;
_SecretString = SecretString;
_OL_ProfileID = OL_ProfileID;
ErrorReturn loginReturn = new ErrorReturn();
try
{
var client = new HttpClient();
var content = new StringContent($"{{\"access\": \"{AccessKeyString}\", \"secret\": \"{SecretString}\"}}", Encoding.Unicode, "application/json");
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("User-Agent", "MyNextBook");
var response = await client.PostAsync(LoginUrl, content);
if (response.IsSuccessStatusCode)
{
var sessionCookie = response.Headers.GetValues("Set-Cookie");
ol_sessionid = string.Join(", ", sessionCookie);
var ss = ol_sessionid.Split(";");
string s = ss[0];
ol_sessionid = s;
//return string.Join(", ", sessionCookie);
loginReturn.success = true;
return loginReturn;
}
else
{
loginReturn.ErrorCode = "GEN-001";
ol_sessionid = "";
Debug.WriteLine($"Login failed with status code: {response.StatusCode}");
loginReturn.ErrorMessage = response.Content.ReadAsStringAsync().Result;
loginReturn.Success = false;
loginReturn.ErrorReason = response.ReasonPhrase;
return loginReturn;
}
}
catch (Exception ex)
{
Debug.WriteLine($"Exception occurred during login: {ex.Message} : {ex.ToString()}");
loginReturn.ErrorCode = "GEN-002";
loginReturn.ErrorMessage = ex.Message;
loginReturn.Success = false;
loginReturn.ErrorReason = ex.ToString();
return loginReturn;
}
}
Getting Started & Contributing
- Setting up your developer environment
- Using
git
in Open Library - Finding good
First Issues
- Code Recipes
- Testing Your Code, Debugging & Performance Profiling
- Loading Production Site Data ↦ Dev Instance
- Submitting good Pull Requests
- Asking Questions on Gitter Chat
- Joining the Community Slack
- Attending Weekly Community Calls @ 9a PT
- Applying to Google Summer of Code & Fellowship Opportunities
Developer Resources
- FAQs: Frequently Asked Questions
- Front-end Guide: JS, CSS, HTML
- Internationalization
- Infogami & Data Model
- Solr Search Engine Manual
- Imports
- BookWorm / Affiliate Server
- Writing Bots
Developer Guides
- Developing the My Books & Reading Log
- Developing the Books page
- Understanding the "Read" Button
- Using cache
- Creating and Logging into New Users
- Feature Flagging
Other Portals
- Design
- Librarianship
- Communications
- Staff (internal)
Legacy
Old Getting Started
Orphaned Editions Planning
Canonical Books Page