Connect to Stack Overflow API [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to connect to the Stack Overflow API as one of my first api calls, but I am struggling.
Can someone tell me why this code does not return a success code?
using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("https://api.stackexchange.com/");
var response = await client.GetAsync("questions");
if (response.IsSuccessStatusCode)
{
}
else
{
Console.WriteLine(response.ToString());
}
}

The response tells you site is required. Hit https://api.stackexchange.com/questions?site=stackoverflow instead.

You're getting back
{"error_id":400,"error_message":"site is required","error_name":"bad_parameter"}
If you read the error and documentation, it needs to know which StackExchange site you want. Try:
https://api.stackexchange.com/questions?site=stackoverflow

Related

NotSupportedException: Cannot serialize interface C# Web Service [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
I'm using for the first time Web Services, and when I try to consume it, I have this error:
NotSupportedException: Cannot serialize interface
I tried changing the configuration names or adding [XmlSerialize] but is not working...
What should I do to avoid this?
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
EndpointAddress endpoint =
new EndpointAddress("myendpoint")
client.ClientCredentials.UserName.UserName = _userName;
client.ClientCredentials.UserName.Password = _password;
SoapClient client = new SoapClient(binding, endpoint);
using (new OperationContextScope(client.InnerChannel)) --> here I have the problem
{
//My code
}

Calculate lines of code change for a commit? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is there a way to calculate lines of code in a Pull Request API in Azure Devops for each file. I have gone through below two links and but was not of much help.
Is there a way to get the amount of lines changed in a Pull Request via the Dev Ops Service REST API?
Lines of Code modified in each Commit in TFS rest api. How do i get?
Thank you.
Steps:
a. Get the commit IDs for the specified pull request
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/commits?api-version=6.1-preview.1
b. Get commit path via the commit ID
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=5.0
c. Get parents commit ID via commit ID
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=5.0
d. Get the result via below API and request body.
POST https://dev.azure.com/{Org name}/_apis/Contribution/HierarchyQuery/project/{Project name}?api-version=5.1-preview
Request Body:
{
"contributionIds": [
"ms.vss-code-web.file-diff-data-provider"
],
"dataProviderContext": {
"properties": {
"repositoryId": "{Repo ID}",
"diffParameters": {
"includeCharDiffs": true,
"modifiedPath": "{Commit path}",
"modifiedVersion": "GC{Commit ID}",
"originalPath": "{Commit path}",
"originalVersion": "GC{parents commit ID}",
"partialDiff": true
}
}
}
}
Result:

Send teams message to a user using c# code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to send teams message to an user using c# code. Is there any way if we can achieve this? If yes please share me some code snippet or the links which will guide me to achieve the same.
Regards,
Satish
For Microsoft Graph API beta you can send a message in a chat:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var chatMessage = new ChatMessage
{
Body = new ItemBody
{
Content = "Hello world"
}
};
await graphClient.Users["{id}"].Chats["{id}"].Messages
.Request()
.AddAsync(chatMessage);
For more info read documentation

Service did not receive API Data through WebClient [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to send data from API to web service. But it is always receiving null
Code in API - WEB CLIENT:
using (var webClient = new WebClient())
{
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
var url = string.Format("End Point URL /SomeAction");
var user= new User()
{
...
};
var data = JsonConvert.SerializeObject(user);
webClient.UploadString(url, data);
}
Service:
public ActionResult SomeAction([System.Web.Http.FromBody]string data)
{
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
FOUsers dataFromXBO =
(FOUsers)json_serializer.DeserializeObject(data);
//statements
}
Please advice, How can i receive data that passed from API
To answer your question directly, to extract a string from a request body, wrap your "string" in another object as per this SO link. ie.
class UserPostRequest{
public string UserJson{get; set;}
}
And change the signature in your controller as follows:
public ActionResult SomeAction([System.Web.Http.FromBody]UserPostRequest data)
With that said, in this specific instance it's likely that you can just do:
public ActionResult SomeAction([System.Web.Http.FromBody]FOUsers data)
and let the API take care of deserialization for you.
EDIT:
After further research, to add completeness to the first half of my answer let me acknowledge that there are many ways to skin that particular cat. The answer provided is just the one I've historically used. Here's another SO post that enumerates other solutions, including accepting a dynamic or HttpRequestMessage in your API method signature. These would be particularly helpful if for some reason you didn't want to change your client code. That said, again, I don't see a particular reason to do manual serialization if you're just going to end up consuming the out-of-the-box functionality.

How can I avoid boilerplate code to validate JWT Token in ASP.NET Web API 2? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
All my REST API methods start with that code as follows:
[HttpPost]
[Route("Login")]
public async Task<IHttpActionResult> Login(QueryModel q)
{
// get JWT Token string form HTTP Header
string token = Request.Headers.GetValues("Authorization").FirstOrDefault();
// decode token
string json = Jose.JWT.Decode(token, JWTModel.secretForAccessToken);
JWTModel jwt = JsonConvert.DeserializeObject<JWTModel>(json);
// check if issued from my homepage.
if (!jwt.iss.Equals("my-home-page.com"))
{
return Content(
HttpStatusCode.Unauthorized,
"access token is not from here"
);
}
// check if it has valid about time
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
if (jwt.iat > now || jwt.exp < now)
{
// request refresh token
return Content(
HttpStatusCode.Unauthorized,
"outdated access token"
);
}
/* ... */
}
How ridiculous and redundant are they!
Can I simplify and modulize them? And How?
(In Node.js, I can solve it by using so called middleware.)
I would suggest you look at DelegatingHandler or if you are running an OWIN based application then you can create Middleware
DelegatingHandler MSDN Link
OWIN Middleware MSDN

Categories

Resources