Web Search using C# Program - c#

I am trying to do a web search from within a C# app. I am currently using this code that gets an error.
WebRequest http = HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)http.GetResponse(); //error occurs here
I keep getting "The remote name could not be resolved: 'search.yahooapis.com'".
Here is the code for the url parameter:
StringBuilder url = new StringBuilder();
url.Append("http://search.yahooapis.com/WebSearchService/V1/webSearch?");
url.Append("appid=YahooDemo&results=100&query=");
url.Append(HttpUtility.UrlEncode(searchFor));
The problem, I think, is that I need an API key from Yahoo in place of 'YahooDemo' in the above code. I went to http://developer.apps.yahoo.com/projects and got an application ID but when I enter it it still does not work? I think the problem is I did not know what to put in the Yahoo project for Application URL and Callback Domain - I don't really know what this even means? I am happy to use other providers such as Google or Bing if this makes it easier. But I am new to C# so really need detailed but simple explanations to understand what I need to do. I am a bit lost. In the end I basically just want to do a web search from my C# program to look for key words, so if their is an easier way to do this I am all for it. Any suggestions?

Related

Accessing OneDrive from Desktop App

I'm trying to get to grips with OneDrive, using this tutorial:
https://msdn.microsoft.com/en-us/library/hh826529.aspx
When I run in code, it gets as far as the makeAccessTokenRequest function, sending the following requestURL:
"https: //login.live.com/oauth20_token.srf?client_id=[myclientID] &client_secret=[myclientsecret]&redirect_uri=https:// login.live.com/oauth20_desktop.srf&grant_type=authorization_code&code=[authcode]"
(please ignore the spaces after "https:", I had to add them here to allow the question)
[myclientid], [myclientsecret], and [authcode] all appear to be populated correctly. It seems to get a response, as it runs the function "accessToken_DownloadStringCompleted", but throws a "TargetInvocationException" error, The inner message of the error is ""The remote server returned an error: (400) Bad Request.".
Could anyone throw any light on this? I'm completely new to this, so apologies if my question makes no sense, or is irritatingly vague..
Requests to the oauth20_token.srf end point need to be a POST with the parameters in the body of the post, instead of the query string. Since you didn't mention what code you're using to build the HTTP request it's hard to provide an example, but take a look at RedeemAuthorizationCodeAsync in my sample OAuth 2 project for an idea.
The outgoing request should look like this:
POST https://login.live.com/oauth20_token.srf
Content-Type: application/x-www-form-urlencoded
client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}&code={code}&grant_type=authorization_code
You may also find this tutorial easier to follow than the one you linked with: https://dev.onedrive.com/auth/msa_oauth.htm.
If you are doing something with OneDrive (you tagged the post OneDrive) then you may want to consider using the OneDrive SDK instead. It includes authentication for several types of .NET projects so you don't need to figure out how to do auth yourself.

Google Freebase Api C# .Net Example

I am new to developing with the Goolge API’s. I am trying to get the Google.Apis.Freebase.V1 API working in C#. Does anyone have a small example on using this API in C#? I have spent the last several days looking and can only find a couple of examples for the old Freebase Api. Nothing for the Google API.
I am just looking for a simple example on setting up a connection to the API, doing a search, then how to handle a MQL query back into a Json object. The simpler the better.
Thanks
Scott
The correct code to do a MQL query in C# using the Google API Client Library should look something like this:
string API_KEY = "your-api-key-here";
FreebaseService service = new FreebaseService{ Key = API_KEY };
String query = "[{\"id\":null,\"name\":null,\"type\":\"/astronomy/planet\"}]";
FreebaseService.MqlreadRequest request = service.Mqlread(query);
string response = request.Fetch();
Console.WriteLine (response);
Unfortunately, there seems to be some sort of error with the client library right now as its not returning any results. I'll try to figure out what's going on there.
Update: The problem appears to be that the client library passes along an alt=json parameter which the Freebase API is unable to support. The Python client library has a way to disable this but there no way to do it in .Net. You can follow the open bug for this on the Google Code project.

How to perform a POST with a web api?

My question is rather simple, but I can't find any information about this on the internet.
I am developing a windows phone application and I want to use the web api (from MVC 4) to get, set, and update.
I already made all the GET methods and they work fine. My question is: How can I perform a POST from a url (and add data to my database)?
Something like this: http://someurl.com/api/post/username/parameter1/parameter2
Is this even possible? And otherwise how else can I resolve this problem?
Simply use RestSharp for all your WebApi work in Windows Phone.
Believe me when I say that it will save you development time!
(To say the truth, I almost never use WebRequest directly in my apps, and just go ahead with RestSharp...)
There are two alternatives:
WebClient.UploadStringAsync
WebClient client = new WebClient();
client.UploadStringCompleted += OnUploadStringCompleted;
client.UploadStringAsync(new Uri("http://someurl.com/api/post/username/parameter1/parameter2", UriKind.Absolute), "Data to upload goes here");
or
HttpWebRequest.BeginGetRequestStream
There's a complete example on this MSDN page.
Hope that helps!

POST to /likes only works after GET - bug or by design?

My app has permissions to "like" something on FB on a user's behalf. Using the Facebook C# SDK (5.4.1), here's what I wrote:
Facebook.FacebookClient fb = new Facebook.FacebookClient(AccessToken);
object o = fb.Get("1234567890_12345678901234567/likes");
dynamic parameters = new ExpandoObject();
dynamic success = fb.Post("1234567890_12345678901234567/likes", parameters);
The second line is superfluous and creates an unnecessary round-trip, as we're not interested in the other "likes" on the same object. However, without it, fb.Post fails and throws "The remote server returned an error: (400) Bad Request."
Is this a bug in the C# SDK, or a bug in the graph API, or is it by design?
Note that it's possible to POST to /comments without a previous GET.
Yes, it should be possible to do post commands without a get. Strangely enough, my like code with the 5.4.1 does not require the extra GET directly before the POST for doing a like. Maybe somewhere else in my app's flow I've already done some sort of GET via the API. However, I'm going to investigate my DELETE problem with me/permissions that I'm encountering (http://stackoverflow.com/questions/8598614/facebook-c-sharp-api-return-400-when-deauthorizing-app) and see if a get to the me/permissions first will help resolve that issue.

Facebook Links without browser

I've read a lot of post on here, and other sites, but still not getting any clarification of my question. So here goes.
I have a Facebook link that requires you to be logged in. Is there a way using .Net (C#) that I can use Facebook API or something to "click" this link without a browser control.
Essentially, I have an application that I wrote that detects certain Farmville links. Right now I'm using a browser control to process the links. However, it's messy and crashes a lot.
Is there a way I can send the url along with maybe a token and api key to process the link?
Does anyone even understand what I'm asking? lol.
Disclaimer: I don't know what Facebook's API looks like, but I'm assuming it involves sending HTTP requests to their servers. I am also not 100% sure on your question.
You can do that with the classes in the System.Net namespace, specifically WebRequest and WebResponse:
using System.Net;
using System.IO;
...
HttpWebRequest req = WebRequest.Create("http://apiurl.com?args=go.here");
req.UserAgent = "The name of my program";
WebResponse resp = req.GetResponse();
string responseData = null;
using(TextReader reader = new StreamReader(resp.GetResponseStream())) {
responseData = reader.ReadToEnd();
}
//do whatever with responseData
You could put that in a method for easy access.
Sounds like your are hacking something... but here goes.
You might want to try a solution like Selenium, which is normally used for testing websites.
It is a little trouble to setup, but you can programmatically launch the facebook website in a browser of your choosing for login, programmatically enter username and password, programmatically click the login button, then navigate to your link.
It is kind of clunky, but get's the job done no matter what, since it appears to facebook that you are accessing their site from a browser.
I've tried similar sneaky tricks to enter my name over and over for Publisher's Clearing House, but they eventually wised up and banned my IP.

Categories