Reputation: 155
I have an application which user Devise+Omniauth to allow the users to signup via Facebook. I am also using Carrierwave to allow the users to upload their own profile image and to process the image requested from Facebook. As such, I have the following functions in the controller and user model:
user.rb
def self.find_for_facebook_oauth( data, signed_in_resource=nil)
user = User.where(:email => data.info.email).first
unless user
params =
{
:user =>
{
:username => data.uid,
:email => data.info.email,
:password => Devise.friendly_token[0,20],
:user_profile_attributes =>
{
:first_name => data.info.first_name,
:last_name => data.info.last_name,
:remote_image_url => data.info.image
},
:user_auths_attributes =>
[{
:uid => data.uid,
:provider => data.provider
}]
}
}
user = User.create!(params[:user])
end
return user
end
omniauth_callbacks_controller.rb
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
Unfortunately, I keep running into this error:
ActiveRecord::RecordInvalid (Validation failed: User profile image could not download file: redirection forbidden: http://graph.facebook.com/813865346/picture?type=square -> https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t5.0-1/1118622_813865346_1465272585_q.jpg):
app/models/user.rb:68:in `find_for_facebook_oauth'
app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook'
Where line 68 is user = User.create!(params[:user])
Logging the params[:user]
provides the following values:
Params: {:username=>"*", :email=>"*", :password=>"iePVLt7XEWk4YwPjja6n", :user_profile_attributes=>{:first_name=>"*", :last_name=>"*", :remote_image_url=>"http://graph.facebook.com/*/picture?type=square"}, :user_auths_attributes=>{:uid=>"*", :provider=>"facebook"}}
I would like some help on getting past this error.
Upvotes: 6
Views: 4263
Reputation: 410
I agree with answer Sandeep Laxman, the problem is http
must replace with https
, because is secure image from Facebook, we can use gsub
#ref ruby-doc
user.remote_avatar_url = auth.info.image.gsub('http:','https:')
or use sub
user.remote_avatar_url = auth.info.image.sub('http:','https:')
Upvotes: 0
Reputation: 285
The omniauth-facebook strategy has an option to change the picture url to a secure https url.
secure_image_url: Set to true to use https for the avatar image url returned in the auth hash.
For example, the picture with a secure url would be requested in devise.rb or omniauth.rb this way:
provider :facebook, 'secrets', 'secrets', :secure_image_url => true
https://github.com/mkdynamic/omniauth-facebook#configuring
Upvotes: 13
Reputation: 413
I was facing the same problem. It seems that the issue was http
redirecting to https
. So I replaced them using gsub
as follows:
user.remote_avatar_url = auth.info.image.gsub('http://','https://')
Upvotes: 27