I'm trying to get user's info via Gem Koala Facebook API with below code
#graph = Koala::Facebook::API.new(auth_hash.credentials.token)
profile = #graph.get_object("me")
user.update(
url: auth_hash.extra.raw_info.id,
email: auth_hash.extra.raw_info.email,
friends: #graph.get_connections("me", "friends"),
birthday: #graph.get_object("me", "birthday")
)
friends: #graph.get_connections("me", "friends") works perfectly fine, I will get a list of friends.
However birthday: #graph.get_object("me", "birthday") will give me type: OAuthException, code: 2500, message: Unknown path components: /birthday [HTTP 400]
Things that returns arrays such as likes, photos works fine.
But strings such as last_name, name, first_name will give me the same error.
What can I do to fix this? Is there something else I can enter instead of just birthday
Although it is not very well documented, I read the code of the gem.
Here it explains how to do what you want.
You can do it like this:
Koala::Facebook::API.new(ACCESS_TOKEN).get_object(:me, { fields: [:birthday]})
This will return the birthday and the id of the user.
birthday is not an endpoint like friends, it´s just a field in the user table. Same for name, first_name and last_name:
/me?fields=name,birthday,first_name,last_name
I have no clue about Ruby and Koala, but i assume you can add the fields parameter as object - this is what it looks like in the docs:
#graph.get_object("me", {}, api_version: "v2.0")
Source: https://github.com/arsduo/koala
I think this should do it!
rest = Koala::Facebook::API.new(access_token)
rest.get_object("me?fields=birthday")
Related
I'm trying to create a Mailchimp list using Mailchimps API (v3) and the REST Client gem. I've got this working correctly for retrieving list names and ids. However, I'm getting a 401 Unauthorized - API Key Missing response for the creation of a list. I think my post request is malformed but I'm having trouble indentifying where I'm going wrong. My code looks like this:
params_hash = {
name: "#{territory}",
contact: {
company: "Company",
address1: "Address",
city: "City",
state: "State",
zip: "0000",
country: "US"
},
permission_reminder: "You are receiving this email because....",
campaign_defaults: {
from_name: "From Name",
from_email: "contact#contact.com",
subject: "Subject",
language: "en"
},
notify_on_subscribe: "contact1#contact.com",
notify_on_unsubscribe: "contact1#contact.com",
email_type_option: true,
apikey: mailchimp_key
}
RestClient.post("#{mailchimp_url}/lists", { params: params_hash }) { |response, request, result, &block|
}
You should not pass your API key in the payload, instead, you should use HTTP Basic Authentication. RestClient does support this, but it's kind of awkward. If you want to use the RestClient shortcuts, you can modify your URL to include the username/password, like this: https://username:api_key#us1.api.mailchimp.com -- username doesn't matter to MailChimp, but it's required for Basic Auth, so you can pass anything.
Alternately, you can use RestClient's Request class directly, making your request something like this instead:
RestClient::Request.execute(method: :post, url: "#{mailchimp_url}/lists", payload: params_hash, user: 'anything', password: mailchimp_key)
But, honestly? RestClient is not great. I prefer HTTParty, which allows you to create very lightweight wrappers with lots of defaults set for you, OR use it like RestClient, but with a more friendly API:
HTTParty.post("#{mailchimp_url}/lists", body: params_hash, basic_auth: {username: 'whatever', password: mailchimp_key})
I use Ruby on Rails 4.2.5 with the Restforce gem for Salesforce API. I create a Contact in my controller :
class CandidateFormController < ApplicationController
def index
client = Restforce.new(
:username => ENV['SALESFORCE_USERNAME'],
:password => ENV['SALESFORCE_MDP'],
:security_token => ENV['SALESFORCE_TOKEN'],
:client_id => ENV['SALESFORCE_APIKEY'],
:client_secret => ENV['SALESFORCE_SECRET'],
)
new_client = client.create('Contact', FirstName: #first_name,
LastName: #last_name,
Email: #email,
MobilePhone: #telephone,
Description: #champ_libre,
Profil_LinkedIN__c: #linkedin
)
end
end
I have a relationship between two of my tables.
Candidate is associated to Opportunity (a job offer if you prefer), and the restforce documentation doesn't explain how to create a new entry with a relation between two elements, or if it does I am not enough experimented to have understand how am I supposed to do so.
I have not enough credit to post screenshots, but if this is necesseray I can use imgur or something like that.
P.S : I already see this post on the subject, but that didn't help me at all.
Well, after another hour of research I finally find how to create relationship in salesforce.
My code looks like this now :
class CandidateFormController < ApplicationController
def index
client = Restforce.new(
:username => ENV['SALESFORCE_USERNAME'],
:password => ENV['SALESFORCE_MDP'],
:security_token => ENV['SALESFORCE_TOKEN'],
:client_id => ENV['SALESFORCE_APIKEY'],
:client_secret => ENV['SALESFORCE_SECRET'],
)
new_client = client.create('Contact', FirstName: #first_name,
LastName: #last_name,
Email: #email,
MobilePhone: #telephone,
Description: #champ_libre,
Profil_LinkedIN__c: #linkedin
)
new_candidature = client.create(
'Candidatures__c',
Candidats__c: "someId",
Offredemploi__c: new_client
)
end
end
In my case, I wanted to create a relationship between an Opportunity and a Contact (A job offer and a Candidate).
I look more into fields that already was created and filled for Opportunity and Contact in my salesforce account, and find out that there were no field that was corresponding to the relationship I was looking for.
I discover than in salesforce, there are objects that exist just for the junction between two objects, and they are called "Junction Object" (pretty obvious, but not easy to find).
You just have to create a new salesforce object with create() after the creation of your first element (a Contact for me) and create the junction object with the good type (for me it was Candidatures__c), and specify the two relationships.
In my own code I create an new Candidature__c, I specify the Id of the job offer (the Candidats__c id) and the Id of the candidate in Offredemploi__c with the Id I create some lines above.
Hope it will helps.
I am building a script to scrape data from a website. You can see the full code here: Undefined method 'click' for nil:NilClass (Mechanize)
Anyways, I have trouble to save this metadata into the database:
members = member_links.map do |link|
member = link.click
name = member.search('title').text.split('|')[0]
institution = member.search('td~ td+ td').text.split(':')[0]
dob = member.search('.birthdate').text.strip[1..4]
{
name: name.strip,
institution: institution.strip,
dob: dob,
bio: bio
}
end
Thus, how can I accomplish this?
Well, it should be as simple as:
Datum.create!(
name: name.strip,
institution: institution.strip,
dob: dob,
bio: bio
)
This is very basic Rails knowledge, make sure you've read Active Record Basics guide.
I have been using Omniauth to retrieve an environment that contains the steam username.
The output looks like this:
<OmniAuth::AuthHash::InfoHash image="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d4/d45a66fee7932d270ec32d4457d865b485245cf1_medium.jpg" location="YT, CA" name="Daiki" nickname="Corybantic Walrus" urls=#<OmniAuth::AuthHash FriendList=#<URI::HTTP:0x0000000614f590 URL:http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=4A8837BF7A6C439681B57F4962D8B011&steamid=76561198128055024&relationship=friend> Profile="http://steamcommunity.com/id/hatterkiller/">> provider="steam" uid="76561198128055024">
I don't really know how to format this inside Stack Overflow, so here is a cleaner Pastebin.
The information I need is inside the <Omniauth::AuthHash::Infohash thing of the code. Is there a way to use Ruby to retrieve the username (nickname) and to put it inside an array?
Sort of like this, but this only worked for the previous output format:
auth = request.env['omniauth.auth']
session[:current_user] = { :nickname => auth.info['nickname'],
:image => auth.info['image'],
:uid => auth.uid }
Something like this:
session[:current_user] = {
nickname: auth[:info][:nickname],
image: auth[:info][:image],
uid: auth[:uid]
}
I'm trying to send this string to my Stripe account to process a transaction, and I want to send both an ID of the item being bought and the amount paid for as a JSON string. How do I include the pieces of the model in the string?
customer = Stripe::Customer.create(email: email, description: {"amount:" amount, "id:" idea_id}')
One part of the model is amount and the other is idea_id
Assuming your model is called Product
#product = Product.find(params[:id])
customer = Stripe::Customer.create(email: email, description: {amount: #product.amount, id: #product.idea_id}.to_json)
or you can also use
customer = Stripe::Customer.create(email: email, description: #product.as_json(only: [:amount, :idea_id]))
However this may not work if you absolutely require the key for idea_id to be 'id'. in which case use the first option.
Hope this helps