Facebook Post Hyperlink Appears Plain Text - c#

I have the following code snippet to post on facebook wall. But when I post hyperlink, appears plain text.
public static bool WriteOnFace(string message, string accessToken)
{
string url= "";
WebClient wc = new WebClient();
url= "https://graph.facebook.com/feed?access_token=" + accessToken + "&message=" + message + "&method=post";
wc.DownloadString(url);
}
I'm not using facebook C# sdk. How to Post Hyperlink?

Try using Facebook C# sdk and instantiating a FacebookClient with your access token and then calling the postasync method on it with a link parameter. Something along these lines should work:
FacebookClient client = new FacebookClient('use your access token');
var args = new Dictionary<string, object>();
args["link"] = "[your link URL]";
client.PostAsync("friend's id/feed",args);
Hope this helps!

Related

403 Forbidden error while invoking API in c# code

This API is working through postman.
I am trying the same thrid-party API from my application like below:
string requestUrl = string.Empty;
string result = string.Empty;
System.Net.WebClient client = new System.Net.WebClient();
using (client)
{
requestUrl = "https://api.fyndx1.de/hogwarts/aggregators/api/v1/config1/authToken";
client.QueryString.Add("username", "tester");
client.QueryString.Add("password", "pwd123");
result = client.DownloadString(requestUrl);
}
403 error is coming. I tried to add User agent to header parameters after querystring but no use.
client.Headers.Add("User-Agent: Other");
Any help is appreciated. Thanks.

Get instagram full profile information without "Instagram Graph Api"

I wanna get some data from the Instagram users.
So I've used Instagram Basic Display Api and the profile data I could receive was these:
username
media count
account type
but I want these data:
username
name
media count
Profile Image
followers count
following count
I don't know how can I have these data without Instagram Graph API(in any way) in c#?
Or is there any way to get these data with the WebClient class or anything like that?
Update for #Eehab answer: I use RestClient and WebClient in this example and both of them give the same result.
Now see WebClient example:
WebClient client = new WebClient();
string page = client.DownloadString("https://www.instagram.com/instagram/?__a=1");
Console.WriteLine(page);
Console.ReadKey();
and see an image of this code here.
now see the result of the code above here
I've also got, that this link is the only access for login users and I've been login into my Instagram account in chrome already, but I think WebClient needs to log in too.
Edit Through #Eehab answer:
In this case for using this Url(https://www.instagram.com/{username}/?__a=1), we can't do it without Instagram logged-in browser profile. So we should log in to Instagram with selenium and use the logged-in cookies to use it for Url requests. So first Install the selenium web driver and then write the following codes(untested):
var driver = new ChromeDriver();
//go to Instagram
driver.Url = "https://www.instagram.com/";
//Log in
var userNameElement = _driver.FindElement(By.Name("username"));
userNameElement.SendKeys("Username");
var passwordElement = _driver.FindElement(By.Name("password"));
passwordElement.SendKeys(Cars[0].auth.pass);
var loginButton = _driver.FindElement(By.Id("login"));
loginButton.Click();
//Get cookies
var cookies = driver.Manage().Cookies.AllCookies.ToList();
//Send request with given cookies :)
var url = "https://www.instagram.com/{username}/?__a=1";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
foreach(var cookie in cookies){
httpRequest.Headers["Cookie"] += $"{cookie.Name}={cookie.Value}; ";
}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
//...
If anyone can improve this question for more uses can edit and I really appreciate it :)
You could do that using the open API , example :
https://www.instagram.com/instagram/?__a=1
example code from postman code :
var client = new RestClient("https://www.instagram.com/instagram/?__a=1");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
you could use HttpClient class also, if you want to use WebClient you could do it with
WebClient.DownloadString Method while I don't recommend using WebClient for this scraping, keep in mind Instagram may block you if blocked you , you need residential proxies to bypass the block.
the response will be json data , use Json.Net or similar library to deserialize it.
just replace instagram with any username you want in the given url.

C# - login to website, $_POST and cookies

I'm trying to login to website and download some pages as it see logged user in C#.
I have class, where are functions to send POST to login page to login. I have verified, that I actually log in, but data's are not keeped, so when I download HTML of the page I need, I get page saying to log in.
I found this, but I don't know how to implement it.
I have class with two funtions: Login() used to login to website, based on this example:
using(WebClient client = new WebClient())
{
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("param1", "<any> kinds & of = ? strings");
reqparm.Add("param2", "escaping is already handled");
byte[] responsebytes = client.UploadValues("http://localhost", "POST", reqparm);
string responsebody = Encoding.UTF8.GetString(responsebytes);
}
Then, second function DownloadHtml(string url), contains this:
using (WebClient client = new WebClient()) {
client.Encoding = Encoding.UTF8;
html = client.DownloadString(url);
return html;
}
How to save cookis in Login() and use them in DownloadHtml() to see the page as logged user? Or shouldn't I use WebClient? If no, what should I use?
Thanks.
After you receive cookies from your first request to authorization you should saved them (in some variable perhaps) and then manually add them to any subsequent request that you will be make.
Adding cookies to request can be made like this:
using (WebClient client = new WebClient())
{
client.Headers.Add("Cookie", "AUTH_COOKIE_NAME=" + AUTH_COOKIE_VALUE);
client.Encoding = Encoding.UTF8;
html = client.DownloadString(url);
return html;
}

How to put account info into facebook login form and click submit button in web browser control?

as the title ! Who can help me this problem ?
thanks!
i tried code:
HtmlDocument put = this.webBrowser1.Document;
put.GetElementById("input").SetAttribute("value", "abc");
i can see element username but password unknow element???
but not work for me! :(
The right way to do this would be to send an automated post request to the server with the parameters that you have specified. You will get HTML in return that you can parse then.
Use WebClient for this.
Here is an example:
string URI = "http://www.server.com/login";
string myParameters = "username=value1&password=value2";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}

uploading a video file to Facebook with description and action links

In C#, I'm uploading a video file to Facebook from a server, using the following method:
string fullurl = "https://graph-video.facebook.com/me/videos?" + "title=" + title + "&access_token=" + accessToken;
WebClient client = new WebClient();
byte[] returnBytes = client.UploadFile(fullurl, path);
path is the path to the video file on the server. This works, and shows a video uploaded post on the user profile. How can I add a text description (with link) and action links to that post?
please keep the answers in C#
you should pass extra query string parameter - description - with embedded URL. Something like this:
var description = "Bring your conversations to life on Facebook. With face-to-face video calling, now you can watch your friends smile, wink and LOL.\n\nTo get started, visit http://www.facebook.com/videocalling";
string fullurl = string.Format("https://graph-video.facebook.com/me/videos?title={0}&description={1}&access_token={2}", HttpUtility.UrlEncode(title), HttpUtility.UrlEncode(description ), accessToken);
I did it. Following is my code:
string a = "User_Access_Token";
string fullurl = string.Format("https://graph-video.facebook.com/me/videos?title={0}&description={1}&access_token={2}", HttpUtility.UrlEncode("Dulha"), HttpUtility.UrlEncode("hello"), a);
WebClient client = new WebClient();
byte[] returnBytes = client.UploadFile(fullurl, #"C:\Users\Users\Downloads\Dulha.mp4");

Categories

Resources