Skip to content

Unable to get started using example code in the readme #10

@iAmWillShepherd

Description

@iAmWillShepherd

When I attempt to run 👇🏾 this code...

Ruby program
require 'dotenv/load'
require 'time'
require 'onesignal'

rest_api_key = ENV['ONESIGNAL_REST_API_KEY']
app_id = ENV['ONESIGNAL_APP_ID']

# setup authorization
OneSignal.configure do |config|
  # Configure Bearer authorization: app_key
  config.app_key = rest_api_key
end

api_instance = OneSignal::DefaultApi.new
notification = OneSignal::Notification.new({ app_id: app_id })

begin
  # Create notification
  result = api_instance.create_notification(notification)
  p result
rescue OneSignal::ApiError => e
  puts "Error when calling DefaultApi->create_notification: #{e}"
end

I get the following error...

API Error message recieved
Error when calling DefaultApi->create_notification: Error message: the server returns an error
HTTP status code: 400
Response headers: {"date"=>"Mon, 07 Mar 2022 20:27:09 GMT", "content-type"=>"application/json; charset=utf-8", "status"=>"400 Bad Request", "ratelimit-limit"=>"6600", "access-control-allow-origin"=>"*", "cache-control"=>"no-cache", "referrer-policy"=>"strict-origin-when-cross-origin", "x-xss-protection"=>"1; mode=block", "ratelimit-remaining"=>"6625", "x-request-id"=>"7ceea0e7-4cdf-4555-890d-9b06fad52ec5", "access-control-allow-headers"=>"SDK-Version", "ratelimit-reference"=>"https://documentation.onesignal.com/docs/disabled-apps", "x-runtime"=>"0.012420", "x-frame-options"=>"SAMEORIGIN", "x-content-type-options"=>"nosniff", "ratelimit-reset"=>"1646684829", "x-envoy-upstream-service-time"=>"14", "vary"=>"Accept-Encoding", "cf-cache-status"=>"DYNAMIC", "expect-ct"=>"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", "server"=>"cloudflare", "cf-ray"=>"6e8611f8a9c1e02b-DFW", "alt-svc"=>"h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"}
Response body: {"errors":["You must include which players, segments, or tags you wish to send this notification to."]}
Response headers
Response headers: {
  "date"=>"Mon, 07 Mar 2022 20:14:39 GMT",
  "content-type"=>"application/json; charset=utf-8",
  "status"=>"400 Bad Request",
  "ratelimit-limit"=>"6600",
  "access-control-allow-origin"=>"*",
  "cache-control"=>"no-cache",
  "referrer-policy"=>"strict-origin-when-cross-origin",
  "x-xss-protection"=>"1; mode=block",
  "ratelimit-remaining"=>"6625",
  "x-request-id"=>"b8e64a6f-f0be-4e67-abdb-3afedeae777a",
  "access-control-allow-headers"=>"SDK-Version",
  "ratelimit-reference"=>"https://documentation.onesignal.com/docs/disabled-apps",
  "x-runtime"=>"0.008245",
  "x-frame-options"=>"SAMEORIGIN",
  "x-content-type-options"=>"nosniff",
  "ratelimit-reset"=>"1646684079",
  "x-envoy-upstream-service-time"=>"10",
  "vary"=>"Accept-Encoding",
  "cf-cache-status"=>"DYNAMIC",
  "expect-ct"=>"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
  "server"=>"cloudflare",
  "cf-ray"=>"6e85ffa63c1f93eb-DFW",
  "alt-svc"=>"h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"
}
Response body
Response body: {
  "errors": [
    "You must include which players, segments, or tags you wish to send this notification to."
  ]
}

The error indicates I must include which players, segments, or tags I wish to send this notification to. The problem is no documentation specifying how to do this.

Other notes

I had to change the line config.access_token = rest_api_key to config.app_key = rest_api_key because the method doesn't exist. I'm not sure about Ruby's nomenclature, but shouldn't this be an attribute?

Error message when using access_token
fun-with-onesignal.rb:8:in `block in <main>': undefined method `access_token=' for #<OneSignal::Configuration:0x000000013a887fa8 @scheme="https", @host="onesignal.com", @base_path="/api/v1", @server_index=0, @server_operation_index={}, @server_variables={}, @server_operation_variables={}, @timeout=0, @client_side_validation=true, @verify_ssl=true, @verify_ssl_host=true, @params_encoding=nil, @cert_file=nil, @key_file=nil, @debugging=false, @inject_format=false, @force_ending_format=false, @logger=#<Logger:0x000000013a887df0 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x000000013a887cb0 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x000000013a887ad0 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @binmode=false, @mon_data=#<Monitor:0x000000013a887a08>, @mon_data_owner_object_id=260>>> (NoMethodError)
        from /Users/iamwill/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/onesignal-1.0.0.beta1/lib/onesignal.rb:69:in `configure'
        from fun-with-onesignal.rb:6:in `<main>'

I inspected the configuration object code 👇🏾 and couldn't find any mention of access_token, so I used app_key. The README should be clarified and include all details needed to get a working example. Ideally, the getting started instructions have a code sample that works. It's not a great experience reading the source to figure out how to get this to work.

https://github.com/OneSignal/onesignal-ruby-client/blob/aa6601f850f71d9a272213dd1f721757a05d99b5/lib/onesignal/configuration.rb#L14-L115

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions