-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Access Key not working with REST API #89
Comments
I ended up creating a minimal client for my simple use case. Perhaps it will help you... require "net/http"
require "json"
# The messagebird-rest-api gem uses the old REST api which isn't supported.
# There is no gem for the new API.
class BirdService
MESSAGES_BASE_URL = "https://api.bird.com/workspaces/%{workspace_id}/channels/%{channel_id}/messages".freeze
ACCESS_KEY = Rails.application.credentials.dig(:bird, :access_key)
WORKSPACE_ID = Rails.application.credentials.dig(:bird, :workspace_id)
SMS_CHANNEL_ID = Rails.application.credentials.dig(:bird, :sms_channel_id)
def initialize(workspace_id, channel_id, access_token)
@workspace_id = workspace_id
@channel_id = channel_id
@access_token = access_token
end
@@client = BirdService.new(WORKSPACE_ID, SMS_CHANNEL_ID, ACCESS_KEY)
def self.client
@@client
end
def send_sms(receiver_number, message_text)
uri = URI(format(MESSAGES_BASE_URL, workspace_id: @workspace_id, channel_id: @channel_id))
request_body = {
receiver: {
contacts: [
{ identifierValue: receiver_number }
]
},
body: {
type: "text",
text: { text: message_text }
}
}
begin
response = Net::HTTP.post(uri, request_body.to_json, headers)
if response.is_a?(Net::HTTPSuccess)
handle_success(response)
else
handle_error(response)
end
rescue StandardError => e
handle_exception(e)
end
end
private
def headers
{
"Content-Type" => "application/json",
"Authorization" => "Bearer #{@access_token}"
}
end
def handle_success(response)
message_data = JSON.parse(response.body)
message_id = message_data["id"]
status = message_data["status"]
created_at = message_data["createdAt"]
puts "Message sent successfully!"
puts "Message ID: #{message_id}"
puts "Status: #{status}"
puts "Created At: #{created_at}"
end
def handle_error(response)
error_data = JSON.parse(response.body)
error_message = error_data["error"] || "Unknown error"
puts "Error sending message: #{response.code} - #{error_message}"
end
def handle_exception(exception)
puts "Exception occurred while sending message: #{exception.message}"
end
end |
Thank you! I did open a support case with Bird, I'm curious where that ends. Edit: Got an update that the REST API is considered legacy and new signups for this API are no longer possible. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The access keys I just created (at https://app.bird.com/settings/access-keys ) do not work with the REST API.
I can send a curl that includes the key in the header (Authorization: AccessKey <access_key>) and successfully sends an SMS. This uses the API at "https://api.bird.com"
But when I use the same key on the REST API ("https://rest.messagebird.com") I get an "incorrect access_key" error. I am using the official Ruby SDK (this one) to hit the REST API.
Do I need a different access key? Is the REST API still supported?
Example failing code:
results in error:
messagebird/client.rb:95:in `parse_body': Request not allowed (incorrect access_key) (error code: 2, parameter: access_key) (MessageBird::ErrorException)
The text was updated successfully, but these errors were encountered: