How do you directly access the Omniauth facebook image url? - ruby-on-rails

Using Omniauth version 1.0.2, currently when I call env["omniauth.auth"]["info"]["image"] to get the image for the current user I get a URL:
http://graph.facebook.com/100002739564577/picture?type=square
This redirects to the actual jpeg url which is what I want:
http://profile.ak.fbcdn.net/hprofile-ak-ash2/532749_100003719364175_332972681_a.jpg
Is it possible to get the jpeg url directly in rails?
Thanks

You don't necessarily have to if you want to display the image.
This works:
<img src="http://graph.facebook.com/100002739564577/picture?type=square" />
If you must however, try this:
url = URI.parse('http://graph.facebook.com/100002739564577/picture?type=square')
res = Net::HTTP.get_response(url)
res['location'] #returns the image URL

Related

Image is not shown in email MVC

I have a strange problem.
I need to send an e-mail from an MVC site. I have an html template for the email, which looks like this(the image part):
...
<img class="auto-style4" src="{PictureSrc}" /><br />
<img src="data:image/png;base64,{pictureBase64}">
....
From the controller I'm replacing the parameters like this:
case "PictureSrc":
string imagePath = "";
imagePath = "~/Images/email-logo.png";
//string base64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(HttpContext.Server.MapPath(imagePath)));
//content = content.Replace("{pictureBase64}", base64);
content = content.Replace("{" + property + "}", HttpContext.Server.MapPath(imagePath));
break;
1.First I have tried to add the path for the image. This way works on my local machine, but not on the server where the live site is.
I've tried to add the URL for the image. WhenI have pasted the URL in to the browser, the image was shown, but in the sent email the image was not shown, not in my local machine, not on the server.
The interesting thing is that I'm sending these mails to outlook accounts. There the images are not shown, but if I send the mail to my yahoo account there the image is shown.
I've tried to convert image to base 64, as you can see on the code and replacing this way the image src. This way again the image was not shown, not on my local machine, not on the server side if I send the mail to outlook accounts. If I send the mail to a yahoo account, the mail is shown.
Can you please advise what can I try in order to resolve this problem?
I ran into this same issue with Outlook a few months ago. While researching, I learned that Outlook tends to strip emails of embedded images. I resolved it by using the CID technique. This technique consists of attaching the image to the email and referencing it in the email template with the image's CID.
First, you would need to get your image's CID. There are plenty of CID converters online to do this. Then, you just need to use the CID in the img src attribute of your template: <img src="cid:" + yourCID + "/>".
Note: this WILL increase the size of your email due to the image being attached.

Get a non-expiring, non-secure URL from S3 gem

My app uploads an image to S3 and returns the URL of that image. However, it returns a URL like http://my-client-dev.s3.amazonaws.com/gifs/export_gif_3_1438603398.gif?AWSAccessKeyId=AKILLV5BH1BNAT3D3D3Q&Expires=1438607003&Signature=SBQmnULXR5F5cAtyZgqbFl0%2Bn%2Fk%3D
How can I get it to return the simple URL that doesn't require access key and signature in the URL? Also, the links expire pretty quickly.
Here is my code:
bucket.objects[filename].write(result.to_blob, {:acl=>:public_read})
url = bucket.objects[filename].url_for(:read, :secure => false).to_s
Have you looked at the S3 SDK? It has a #public_url method.

How to get LinkedIn profile picture using OAuth?

Users can log in to my Rails app using their LinkedIn account thanks to OAuth. However, I am having trouble displaying the user's profile image. The following URL does not load a picture:
<%= image_tag("http://api.linkedin.com/v1/people/{user-id}/picture-url") %>
How can I get the user's LinkedIn profile image to display in my Rails app?
Thanks!
Try to get the original picture with:
http://api.linkedin.com/v1/people/{user-id}/picture-urls::(original)
Update:
From current docs (recommend to read it):
Using current user (after user logged in):
http://api.linkedin.com/v1/people/~:(picture-url)
Using member_id:
http://api.linkedin.com/v1/people/id=12345:(picture-url)
Public profile:
http://api.linkedin.com/v1/people/url=<public-profile-url>:(picture-url)
Those URLs return xml, so you could parse the xml response to get picture-url string and use it as a param for image_tag. Alternatively, you can retrieve info as a json passing an extra param like:
http://api.linkedin.com/v1/people/~:(picture-url)?format=json
In both cases (xml or json), you need to extract the picture-url from api response for passing it to image_tag.
This gem omniauth-linkedin-oauth2 could probably help you.
This is my solution working perfectly:
def callback(self):
self.validate_oauth2callback()
oauth_session = self.service.get_auth_session(
data={'code': request.args['code'],
'grant_type': 'authorization_code',
'redirect_uri': self.get_callback_url()},
decoder=jsondecoder
)
me = oauth_session.get('people/~:(id,first-name,last-name,public-profile-url,email-address,picture-url,picture-urls::(original))?format=json&oauth2_access_token='+str(oauth_session.access_token), data={'x-li-format': 'json'}, bearer_auth=False).json()
social_id = 'linkedin$' + me['id']
name = me['firstName']
surname = me['lastName']
email = me['emailAddress']
url = me['publicProfileUrl']
image_small = me.get('pictureUrl', None)
image_large = me.get('pictureUrls', {}).get('values', [])[0]
return social_id, name, surname, email, url, image_small, image_large, me

how to show thumbnails for vimeo and youtube embedded links in ruby on rails?

I have embedded the youtube and vimeo links in my site and trying to show the thumbnail of the video as a link to play while on click.
i tried the gem "has_vimeo_video" but it only accepts the vimeo videos.So i want to show the thumbnails of both the videos i.e youtube and vimeo.
Please need solution.
Thanks
the video_info gem is great for fetching thumbnails:
https://github.com/thibaudgg/video_info
I found out yesterday that there was carrierwave-video-thumbnailer which is pretty cool. It basically does what you want
A thumbnailer plugin for Carrierwave. It mixes into your uploader setup and makes easy thumbnailing of your uploaded videos. This software is quite an alpha right now so any kind of OpenSource collaboration is welcome.
But you will need to of course include carrierwave into your application. Hopefully this helps
Carreierwave is an uploader that enables users to upload content in their app, which I think
Update
Alternatively you can use embedly which allows you to add embedded media into your application. The API provides a lot response options as can be seen here. In doing so you could do something like:
$.embedly('http://www.youtube.com/watch?v=_FE194VN6c4',
{maxWidth: 600,
elems: $('#element'),
success: function(oembed, dict){
alert(oembed.title);
});
The elems property is where you want the video to be embedded, so that when the link is pasted in it should embed the video. You need to pass the video url and get it using the jQuery selector from wherever you put your url in your html.
Your other alternative is to have a look at the following auto_html gem
Add a new file 'thumbnail_helper.rb' in '/app/helper'. Copy and paste the following code in that file:
module ThumbnailHelper
# Regex to find YouTube's and Vimeo's video ID
YOUTUBE_REGEX = %r(^(http[s]*:\/\/)?(www.)?(youtube.com|youtu.be)\/(watch\?v=){0,1}([a-zA-Z0-9_-]{11}))
VIMEO_REGEX = %r(^https?:\/\/(?:.*?)\.?(vimeo)\.com\/(\d+).*$)
# Finds YouTube's video ID from given URL or [nil] if URL is invalid
# The video ID matches the RegEx \[a-zA-Z0-9_-]{11}\
def find_youtube_id url
url = sanitize url
matches = YOUTUBE_REGEX.match url.to_str
if matches
matches[6] || matches[5]
end
end
# Finds youtube video thumbnail
def get_youtube_thumbnail url
youtube_id = find_youtube_id url
result = "http://img.youtube.com/vi/#{youtube_id}/0.jpg"
end
# Finds Vimeo's video ID from given URL or [nil] if URL is invalid
def find_vimeo_id url
url = sanitize url
matches = VIMEO_REGEX.match url.to_str
matches[2] if matches
end
# Finds vimeo video thumbnail
def get_vimeo_thumbnail url
vimeo_id = find_vimeo_id url
result = URI.open("http://vimeo.com/api/v2/video/#{vimeo_id}.json").read
begin
JSON.parse(result).first['thumbnail_large']
rescue StandardError
nil
end
end
# Main function
# Return a video thumbnail
# If the url provided is not a valid YouTube or Vimeo url it returns [nil]
def get_video_thumbnail(url)
if find_vimeo_id(url)
get_vimeo_thumbnail(url)
elsif find_youtube_id(url)
get_youtube_thumbnail(url)
end
end
end
Now call the get_video_thumbnail('video_url') from your view. It will return you the thumbnail of the given video.
You don't need any gems for such a simple task
url = 'https://vimeo.com/126100721'
id = url.partition('vimeo.com/').last
result = URI.open("http://vimeo.com/api/v2/video/#{id}.json").read
begin
JSON.parse(result).first['thumbnail_large']
rescue StandardError
nil
end

How do I mask Facebook graph api URLs for pictures?

I'm trying to display Facebook profile pictures on my site, but don't want to leak the facebook id's of the people in the source.
For example, this URL: http://graph.facebook.com/4/picture will redirect to: http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg when you load it in a browser. I'd like to get the 2nd url (CDN url) and use it as my img src since it doesn't show the facebook id in the url.
I'm doing this in Ruby on Rails at the moment and am curious if there's a better way that what I have done below:
def picture_square(facebook_id, secure=false)
raw_url = "http://graph.facebook.com/" facebook_id + "/picture?type=square"
if secure
binary_img = ''
open(raw_url) do |f|
binary_img = f.read
end
encoded_img = Base64.encode64(binary_img)
return 'data:image/jpg;base64,' + encoded_img.to_s
else
return raw_url
end
end
You could call this with the following HTML (using the above example):
<img src="<%= picture_square(4, true) %>"
This definitely works and uses the inline image properties to actually render the image, but it's a bit slow if you have a bunch of images that you're trying to load.
Is there a way in Ruby that I can get the redirected URL and just return that instead of trying to get the actual raw binary data and encode it to base64?
Make a call to the graph API with this url:
http://graph.facebook.com/4/?fields=picture&type=large
This will return the image you are looking for inside the json response. The other option would be to make an http request to the first url you posted and then inspect the HTTP headers to read the location header..

Resources