Put_connections to create a new event with Koala? - ruby-on-rails

I'm trying to create a new event using the Koala gem and it's returning with the same error I got when I tried to update an event with an incorrectly formatted datetime value.
I can update just fine now but still cannot create an event.
Here's the code I use on my update method which works:
start_time = safe_params[:start_time].in_time_zone
end_time = safe_params[:end_time].in_time_zone
graph.put_connections(safe_params[:fb_id], "event", {
name: safe_params[:name],
description: safe_params[:description],
privacy: safe_params[:privacy]
})
And here's the code I'm trying to use to create a new event object:
graph.put_connections("/me/events", "event", { #this is the line that errors
name: safe_params[:name],
description: safe_params[:description],
privacy: safe_params[:privacy]
})
According to Facebook's documentation on creating an event (https://developers.facebook.com/docs/graph-api/reference/user/events/), I should be able to create a new event just by initiating a post to /me/events. Anyone have any idea?
I also tried:
graph.put_connections("/"+current_user.fb_id.to_s+"/events", "event", {
Thanks!

What happens if you do something like this?
graph.put_connections("me", "events", {
name: safe_params[:name],
description: safe_params[:description],
privacy: safe_params[:privacy],
start_time: ...,
end_time: ...
})

So after messing with Facebook's Graph Explorer and attempting hundreds of different combinations with put_connections I decided to make a straight graph_call using Koala.
Finally got an ID response back. I almost cried. Thought I'd share with the community in case there's someone else trying to do the same thing.
event_response = graph.graph_call("/me/events",{
name:safe_params[:name],
start_time: safe_params[:start_time],
privacy_type: safe_params[:privacy],
access_token: current_user.oauth_token}, "POST")
safe_params[:fb_id] << event_response["id"]
#event = Event.create safe_params
I make the call in a stored variable event_response because the Facebook Id returned is used in my app.
First thing I found out: despite using "privacy" as the name of the privacy field when GETting from Facebook and saying so in their documentation, "privacy_type" is actually what you want (found this out in another SO post).
The second thing I found out is even though you are authenticated (have a user token) when you make a graph_call you STILL need to pass along the current_user access token along with making a POST graph_call.
Hope this helps someone!

Related

Editing Google Calendar Event Meets URI With External Link via API

I've had code that has been working for the last year or so, that adds a new Google Meets Entry Point with my own custom URI to the Google Calendar Event via the Google Calendar API.
For example if I click on "Join with Google Meet" below it does not go to a meets.google.com link like usual, because I replaced it with my own custom link.
Unfortunately for some reason in the past couple weeks this stopped working. Now when my code tries to edit the URI for the meet, it returns this error: Google::Apis::ClientError (invalid: Invalid Value). I haven't changed the code in months and this only started happening recently.
Here is what the code looks like:
def update_event_meet_url(service, event, send_updates = "all")
entry_points = [
Google::Apis::CalendarV3::EntryPoint.new(
entry_point_type: "video",
label: meeting_url,
uri: meeting_url,
),
]
if original_phone_info.present?
entry_points << Google::Apis::CalendarV3::EntryPoint.new(
entry_point_type: original_phone_info["entry_point_type"],
label: original_phone_info["label"],
uri: original_phone_info["uri"],
pin: original_phone_info["pin"],
region_code: original_phone_info["region_code"],
)
end
event_changes = {
conference_data: Google::Apis::CalendarV3::ConferenceData.new(entry_points: entry_points),
}
updated_event = Google::Apis::CalendarV3::Event.new(event_changes)
service.patch_event(
gcalendar_id,
event.id,
updated_event,
conference_data_version: 1,
send_updates: send_updates,
)
end
I know the problem is with the video entry point because I replaced the meeting_url with a working https://meets.google.com link and it worked. But if I try anything else that is not meets.google.com it errors.
I'm using this Ruby gem https://github.com/googleapis/google-api-ruby-client.
Any help would be appreciated! Thanks!
Contacted google support and looks like there was a recent release that changed the behavior of conferenceData.conferenceSolution.key.type. More info here: https://developers.google.com/calendar/releases#january_11_2021)we
Basically I needed to change my conference solution key type to addOn instead of hangoutsMeet. The subsequent problem I had was that I still needed a hangoutsMeet link, since the third party url I'm using would eventually redirect to the meets.google.com link. And the only way to get a hangoutsMeet link is to create the initial event with conferenceData.conferenceSolution.key.type = hangoutsMeet.
So what I did was create the google event with conferenceData.conferenceSolution.key.type = 'hangoutsMeet' and then I did a subsequent patch_event call that set the initial hangoutsMeet conference solution to null and created a new conferenceSolution of type addOn. That way I could store the original meets.google.com link and then use that link for the redirect from my third party link.
def update_event_meet_url(service, event, send_updates = "all")
entry_points = [
Google::Apis::CalendarV3::EntryPoint.new(
entry_point_type: "video",
label: meeting_url,
uri: meeting_url,
),
]
conference_solution_key = Google::Apis::CalendarV3::ConferenceSolutionKey.new(type: "addOn")
conference_solution =
Google::Apis::CalendarV3::ConferenceSolution.new(key: conference_solution_key)
event_changes = {
conference_data: Google::Apis::CalendarV3::ConferenceData.new(
conference_solution: conference_solution,
entry_points: entry_points,
create_request: nil,
),
}
updated_event = Google::Apis::CalendarV3::Event.new(event_changes)
service.patch_event(
gcalendar_id,
event.id,
updated_event,
conference_data_version: 1,
send_updates: send_updates,
)
end

shopify application charge failing to save

Below is my code for a Shopify one-time-application-charge in Ruby. I followed the shopify "add billing to your app" page (https://help.shopify.com/api/tutorials/adding-billing-to-your-app) for the code, except didn't need a recurring charge. I have also found someone else who posted their one-time-charge code which looks very similar to mine (https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/one-time-application-charge-example-for-shopify-rails-app-489347).
def create_application_charge
application_charge = ShopifyAPI::ApplicationCharge.new(
name: "MyApp",
price: 0.09,
return_url: "https:\/\/myapp.herokuapp.com\/activatecharge",
test: true)
save = application_charge.save
if save
redirect application_charge.confirmation_url
return
end
flash[:error] = "The save worked: #{save}"
end
The flash always responds as false. Is there a failure at authentication that would prevent this? Or something to get the store to accept an application charge? I'm at a loss as to why this does not work.
Any help would be greatly appreciated, thank you.
The primary issue appears to be that the minimum charge you can request is $0.50, for which I wasn't meeting with my choice of using $0.09 for my test.

Retrieve Customer's default and active card from Stripe

I am trying to retrieve the default and active card of a Customer. (Also keep in mind that with the coding I have, the customer can always have one card which means if there is a way around it it can help).
Some months ago I used this code segment which was working fine. It seems Stripe made some updates and I can't get it to work now.
current_user.stripe_card_id = customer.active_card.id
The error I get is
undefined method `active_card' for #Stripe::Customer
If you need any more information please let me know.
edit: customer.default_card.id does not work either.
I used customer.methods to check the methods and found this (default_source):
current_user.stripe_card_id = customer.default_source
Works fine now. Thank you
default card id will available in customer object's "default_source" key
{
"id": "cus_GACkqbqFD8RQw4",
"object": "customer",
"default_source": <DEFAULT CARD ID HERE>
...
}
read more here : https://stripe.com/docs/api/customers
[EDIT] Additionally,
It's worth noting that when you request a list of all the cards belonging to a particular customer, the default card is always at the top of the result. So you could also get the default card by requesting the customers cards and adding a limit of 1.
Information on how to achieve this: https://stripe.com/docs/api/cards/list
PaymentMethods API - 2020 update
If you have switched from old Sources API to the new Payment Methods API then you should know that unlike old Sources there's no default Payment Method for a Customer.
Now, you can attach a Payment Method as default one to a subscription object:
Stripe::Subscription.update(
'sub_8epEF0PuRhmltU',
{
default_payment_method: 'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
}
)
or as a customer.invoice_settings.default_payment_method
Stripe::Customer.update(
'cus_FOcc5sbh3ZQpAU',
{
invoice_settings: {default_payment_method: 'pm_1vvc9v2eZvKYlo2CJDeTrB4n'},
}
)
Here is the whole Stripe documentation on that
Relying on customers' default_source is safe no matter the changes. You can see here that subscriptions will still use customers' default_source if both invoice_settings.default_payment_method and subscription.default_payment_method are not set.
customer = Stripe::Customer.retrieve(customer_id_on_stripe)
first_3_cards = customer.sources.all(limit: 3, object: 'card')[:data]
Will return array of cards, if you want to fetch bank_accounts
first_3_bank_accounts = customer.sources.all(limit: 3, object: 'bank_account')[:data]

koala Facebook events api

I'm trying to use the FB Events API (v1) to publish events which works great.
https://developers.facebook.com/docs/graph-api/reference/v1.0/page/events
Everything works... except, I can't get the no_feed_post method to work.
The Event posts perfectly, but the feed/wall post is NOT suppressed like it's supposed to be.
params = { name: "Blah # #{place.name}", description: event.prizes, location: '123 Blah St.',
start_time: Time.current, no_feed_story: true }
I have tried setting no_feed_story to:
true
1
"true"
t
Nothing seems to work... what does Facebook want?
Scanning the docs on facebook, they indicate these are the valid fields.
name
start_time
end_time
description
location
location_id
privacy_type
I don't see no_feed_story as a POST option
Please also make note there is a note that states:
This document refers to an outdated version of Graph API. Please use the latest version

How to Add Tag via Asana API

I am trying to do a simple Salesforce-Asana integration. I have many functions working, but I am having trouble with adding a tag to a workspace. Since I can't find documentation on the addTag method, I'm sort of guessing at what is required.
If I post the following JSON to https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tasks:
{"data":{"name":"MyTagName","notes":"Test Notes"}}
The tag gets created in Asana, but with blank notes and name fields. If I try to get a bit more fancy and post:
{"data":{"name":"MyTagName","notes":"Test Notes","followers":[{"id":"MY_USER_ID"}]}}
I receive:
{"errors":[{"message":"Invalid field: {\"data\":{\"name\":\"MyTagName\",\"notes\":\"Test Notes\",\"followers\":[{\"id\":\"MY_USER_ID\"}]}}"}]}
I'm thinking the backslashes may mean that my request is being modified by the post, though debug output shows a properly formatted json string before the post.
Sample Code:
JSONGenerator jsongen = JSON.createGenerator(false);
jsongen.writeStartObject();
jsongen.writeFieldName('data');
jsongen.writeStartObject();
jsongen.writeStringField('name', 'MyTagName');
jsongen.writeStringField('notes', 'Test Notes');
jsongen.writeFieldName('followers');
jsongen.writeStartArray();
jsongen.writeStartObject();
jsongen.writeStringField('id', 'MY_USER_ID');
jsongen.writeEndObject();
jsongen.writeEndArray();
jsongen.writeEndObject();
jsongen.writeEndObject();
String requestbody = jsongen.getAsString();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tags');
req.setMethod('POST');
//===Auth header created here - working fine===
req.setBody(requestbody);
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
Any help appreciated. I am inexperienced using JSON as well as the Asana API.
The problem was that I was posting to the wrong endpoint. Instead of workspaces/workspaceid/tags, I should have been using /tags and including workspaceid in the body of the request.
Aha, so you can add tags and even set followers despite the API not mentioning that you can or claiming that followers are read-only.
So to sum up for anyone else interested: POSTing to the endpoint https://app.asana.com/api/1.0/tags you can create a tag like this:
{ "data" : { "workspace": 1234567, "name" : "newtagname", "followers": [45678, 6789] } }
where 1234567 is your workspace ID and 45678 and 6789 are your new followers.
Since you posted this question, Asana's API and developer has introduced Tags. You documentation lays out the answer to your question pretty clearly:
https://asana.com/developers/api-reference/tags
I'm a bit confused by your question. Your ask "how to add a tag" but the first half of your question talks about adding a task. The problem with what you describe there is that you are trying to set a task's followers but the followers field is currently read-only according to Asana's API documentation. That is why you are getting an error. You can not set followers with the API right now.
The second part of your question - with the sample code - does look like you are trying to add a tag. However, right now the Asana API does not support this (at least according to the API documentation). You can update an existing tag but you can't add one.
So, to sum up: at this time the API does not allow you to add followers to a task or to create new tags.

Resources