Detect facebook crawler within mvc/razor view - asp.net-mvc

I am currently working on facebook sharing and it seems there aren't many topics on facebook sharing with C#. Trying to learn something...
I have Open Graph meta tags in one of razor views like below:
<head>
<meta name="viewport" content="width=device-width" />
<title>Hello World!</title>
<meta property="fb:app_id" content="************" />
<meta property="og:site_name" content="www.hello-world.com" />
<meta property="og:type" content="website" />
<meta property="og:url" content="http://hello-world.com/home/fbshare" />
<meta property="og:title" content="How are you doing today?" />
<meta property="og:description" content="Great to know you are doing fine." />
<meta name="author" content="Hello" />
<meta property="og:image" content="https://images.pexels.com/photos/2324/skyline-buildings-new-york-skyscrapers.jpg?w=940&h=650&auto=compress&cs=tinysrgb" />
<meta property="og:image:type" content="image/jpeg" />
<meta property="og:image:width" content="800" />
<meta property="og:image:height" content="420" />
</head>
For normal users I'd like to redirect them to somewhere else from this view like below:
#{Response.Redirect("Somewhere in my application")};
But I do not want to redirect the facebook scraper too. Is there anyway I can identify the facebook or facebook scraper user agent and If I found out that this is facebook user agent do not redirect him anywhere else redirect them from the view to somewhere else.
P.S: According to facebook official documentation I have to find facebook user agent and allow them scrape my Open Graph meta tags but I can't dig a way.
The Facebook crawler needs to be able to access your content in order
to scrape and share it correctly. Your pages should be visible to the
crawler. If you require login or otherwise restrict access to your
content, you'll need to whitelist our crawler. You should also exempt
it from DDoS protection mechanisms.
If content isn't available at the time of scraping, you can force a
rescrape once it becomes available by passing the URL through the
Sharing Debugger.
The Facebook crawler can be identified by either of these user agent strings:
facebookexternalhit/1.1
(+http://www.facebook.com/externalhit_uatext.php)
OR
facebookexternalhit/1.1

Have you tried:
var userAgent = HttpContext.Request.Headers["User-Agent"];
if (userAgent != null &&
userAgent.Contains("facebookexternalhit/"))
{
// Is a Facebook agent
}
else
{
// Is not a Facebook agent
}
Of course, in MVC you should never redirect from a view, only redirect from a controller or a filter.

Related

Sharing to Twitter and LinkedIn with images and descriptions

I need to share images from web links with text to Twitter and LinkedIn.
I am able to add text, but I am not able to add images.
I am using Kentico CMS and I have written the web part transformation as below. But the image is not shown on the Twitter and LinkedIn plugins.
Attached here with the transformation below.
Added the Twitter and LinkedIn result without images.
Can any one guide me with sharing to Twitter and LinkedIn feeds with images?
Thanks
<a class="fb-xfbml-parse-ignore" target="_blank"
href="https://twitter.com/intent/tweet?url=<%=Server.UrlEncode(CMS.DocumentEngine.DocumentContext.CurrentDocument.AbsoluteURL)%>
&picture=<%#GetAbsoluteUrl(CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("StoryImageURL").ToString())%>
&text=<%#CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("Title").ToString()%>"
onClick="return popup(this, 'notes')">
<img src="<%# Eval("twittericon") %>" alt="twitter icon" />
</a>
<a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.linkedin.com/shareArticle?url=<%=CMS.DocumentEngine.DocumentContext.CurrentDocument.AbsoluteURL%>
&picture=<%#GetAbsoluteUrl(CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("StoryImageURL").ToString()) %> <%#CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("BlogDescription").ToString()%>" onClick="return popup(this, 'notes')">
<img src="<%# Eval("linkedicon") %>" alt="linked icon" />
</a>
First of all, I do not think Twitter and LinkedIn expose a way of sharing an image alongside the text just by adding a parameter to the share URL.
For Twitter, you could set up Cards, such as a Summary Card, and add meta tags to your website. By adding a twitter:image meta tag, you can tell Twitter which image to use.
Similar applies to LinkedIn - you would need to use Open Graph meta tags, as there is no way to provide an image as part of the share URL.
Regarding the transformation you wrote - make sure you are rendering the correct data, as using CMS.DocumentEngine.DocumentContext.CurrentDocument inside a transformation would retrieve data from the current document that is being viewed, as opposed to data of the repeated item. For rendering data of the repeated item, you can use the Eval method.
This:
<%#CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("Title").ToString()%>
Would become this:
<%#Eval("Title")%>
For all my sites I use a Head HTML Code webpart on the page template of the pages I want or need to have this Open Graph (OG) data on. Here's a sample of what I put in it to dynamically populate some of the data.
<meta property="og:site_name" content="Kehren Development - Kentico MVP & Experts" />
<meta property="og:url" content="{% CurrentDocument.AbsoluteURL #%}" />
<meta property="og:image" content="{%RootDocument.AbsoluteURL#%}KehrenDev/media/images/logos/twitter-logo-800.jpg" />
<meta property="og:type" content="article" />
<meta property="og:title" content="{% BlogPostTitle %}" />
<meta property="og:description" content="{% StripTags(BlogPostSummary) %}" />
<meta property="article:publisher" content="https://www.facebook.com/bkehren" />
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="{% BlogPostTitle %}">
<meta name="twitter:site" content="#kehrendev">
<meta name="twitter:creator" content="#bkehren">
<meta name="twitter:description" content="{% StripTags(BlogPostSummary) %}">
<meta name="twitter:url" content="{% CurrentDocument.AbsoluteURL #%}">
<meta name="twitter:image" content="{%RootDocument.AbsoluteURL#%}KehrenDev/media/images/logos/twitter-logo-800.jpg">
<meta name="twitter:domain" content="{%RootDocument.AbsoluteURL#%}">
This will be rendered on the page anytime a blog post is viewed. You can set these properties on the webpart to only display for those page types. There are other meta tags you can use or different fields you can set but this will get you started.
Now you can still use the share links you have but only populate the share link with the actual article link and maybe an encoded description, the OG tags will pickup the rest.

Small preview when sharing link on Social media Ruby On Rails

I'm working on a site whose front end is in angularjs and backend in ROR , Same ROR API is used in an android app also . Now I have a situation here . I need to share my Web's post on the Social media like facebook , twitter and google plus . And along with the link to the single post there should be a small preview also (a preview of the post which crawls before posting e.g in facebook) .I did it using angular Plugins . But when it comes to Android side , what they share and what displays on Facebook is the Link only . Then i did some R&D and i came to know that it must be done on server side with social media meta tags . I did a little bit but it still no works . I'm stuck on it . Below is what i've done so far .
def show
#post = Post.find_by_permalink(params[:id])
respond_to do |format|
format.html
format.js
end
end
In My views posts/show.html.erb
<!DOCTYPE html>
<html>
<head>
<title> Shared Question</title>
<meta property="og:title" content="My Page" />
<meta property="og:description" content="A description of my page." />
<meta property="og:image" content="http://www.example.com/images/my_lovely_face.jpg" />
<!-- etc. -->
</head>
<body>
Here is the body
</body>
</html>
I just need to show a small preview whenever some user share my web post's link on any media
You are correct, you need to implement special open graph (and other) tags for the customized social sharing functionalities.
Keep in mind that e.g. facebook will cache your page.
So you may try https://developers.facebook.com/tools/debug/ to test your code without caching - the code itself looks fine.
Example Code:
Place this code between <head> and </head> with your other meta tags.
<meta property="og:title" content="Title">
<meta property="og:site_name" content="Name">
<meta property="og:url" content="URL">
<meta property="og:description" content="Description Text">
<meta property="og:type" content="article">
<!--- network Specific -->
<meta property="fb:app_id" content="AppID">
<meta name="twitter:card" content="app">
<meta name="twitter:site" content="#FernerJohannes">
If you want to target Twitter specifically here are their card-guidelines:
https://dev.twitter.com/cards/getting-started
You may also want to checkout: Open Graph validation for HTML5
You can set a custom image for you website to be shown in fb posts -
Add the OG XML to your html tag
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="http://ogp.me/ns/fb#">
Add these meta tags to set the custom image that OG will use
<meta property="og:image" content="url_to_image" />
You can try reading this for more insight.
Hopefully this helps.

FBSDKAppInviteContent for invite the Facebook friend [duplicate]

I've double checked everything, from my understanding this is all I need to do:
self.inviteContent = [[FBSDKAppInviteContent alloc] initWithAppLinkURL:[NSURL URLWithString:#"http://mywebsite.com"]];
[FBSDKAppInviteDialog showWithContent:self.inviteContent delegate:self];
On mywebsite.com I have this in the heading (example values shown here):
<html>
<head>
<title>My Website</title>
<meta property="al:ios:url" content="appurl://action">
<meta property="al:ios:app_store_id" content="1234567">
<meta property="al:ios:app_name" content="My App">
</head>
<body>
...
</body>
</html>
So the dialog switches over the to the latest Facebook iOS app. I write a message and pick the person I want to send the invite to, hit send and I get this error:
Missing App Link URL
The app link used with this invite does not contain an Android or iOS URL. Developers are required to enter a URL for at least one platform.
What am I doing wrong?
My app handles the custom URL fine because if I enter appurl://action in Mobile Safari it opens my app up.
You need to create an AppLink URL from here/
Choose your application from the list
Fill up the form with equivalent information
Click on Next !
You're done.
Now, you can use that link with your code for invite.
self.inviteContent = [[FBSDKAppInviteContent alloc] initWithAppLinkURL:[NSURL URLWithString:APP_LINK_HERE]];
[FBSDKAppInviteDialog showWithContent:self.inviteContent delegate:self];
The format of that link will be something like https://fb.me/xxxxxxxxxxxxxxxx
All xs will be replace by a digit.
Try to add also og:title and og:type properties, it works for me
<meta property="og:title" content="My App" />
<meta property="og:type" content="website" />
Adding meta tags would solve this, this is snippet of html from app link created via FB. Fb is adding some extra meta tags on it's app link url page, and it also redirects direct to itunes if opened on a browser.
<html>
<head>
<title>app name</title>
<meta property="fb:app_id" content="your fb app id" />
<meta property="al:ios:url" content="Your app link url" />
<meta property="al:ios:app_name" content="your app name" />
<meta property="al:ios:app_store_id" content="your app id" />
<meta property="al:web:should_fallback" content="false" />
<meta http-equiv="refresh" content="0;url=https://itunes.apple.com/WebObjects/MZStore.woa/wa/redirectToContent?id=your app store id" />
</head>
<body>
Redirecting...
</body>
</html>

what should be structure of website to retrieve site information in telegram?

Some website retrieved information in telegram when send link and some website doesn't like this:
So my question is there any structure for website to load information in telegram ?
The Open Graph protocol enables any web page to become a rich object in a social graph like Facebook, twitter, telegram, Skype and ...
As an example, the following is the Open Graph protocol markup for The Rock on IMDB:
<html prefix="og: http://ogp.me/ns#">
<head>
<title>The Rock (1996)</title>
<meta property="og:title" content="The Rock" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
...
</head>
...
</html>
Simply use meta tag with name description:
<head>
<meta name='description' content="some content"/>
</head>

Publish action with Facebook Open Graph

I'm struggling, trying to implement Facebook Open Graph into my iOS application.
On the iOS code side, everything seems to work fine and I get the automatic confirmation msg that the Open Graph action has been published.
What is weird is that even if I edit the .html object on my server, the object published on facebook doesn't change. I always get the same object again and again. Does anybody know how is that possible?
This is how my object looks like:
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# object: http://ogp.me/ns/object#">
<title>Group App</title>
<meta property="og:title" content="Test" />
<meta property="og:image" content="http://www.groupapp.org/ita/attuale/icona.png" />
<meta property="og:determiner" content="" />
<meta property="fb:app_id" content="172461289531661" />
<meta property="og:url" content="https://itunes.apple.com/us/app/ruzzle/id504265646?mt=8" />
<meta property="og:type" content="groupappitalia:object" />
Basically, I edit title, image and url; but nothing changes. Always get the same result.
<meta property="og:url" content="https://itunes.apple.com/us/app/ruzzle/id504265646?mt=8" />
That’s the actual address of your OG object. And so naturally this is also where Facebook retrieves the Open Graph info from, because that’s how the Open Graph works.
So you would have to change the attributes of that URL, and not your own page, to have these changes reflected on Facebook.

Resources