twilio / twilio-ruby

A Ruby gem for communicating with the Twilio API and generating TwiML
MIT License
1.35k stars 462 forks source link

Impossible to create messaging services through AccountInstance resource #661

Open rafaeelaudibert opened 1 year ago

rafaeelaudibert commented 1 year ago

Issue Summary

We are trying to create messaging services on a subaccount. At first, we tried doing so following what is suggested on the README, by instantiating a client containing the subaccount_sid and then hitting the messaging API

client = Twilio::REST::Client.new(account_sid, auth_token, subaccount_sid)
client.messaging.v1.services.create(friendly_name: "Test Subaccount")

That does not work because the gem uses the main account SID instead of the subaccount SID in the request path. That's already been explained on https://github.com/twilio/twilio-ruby/issues/225.

When trying the workaround on that issue we found the actual issue here: messaging is not defined on the AccountInstance class https://github.com/twilio/twilio-ruby/blob/e6ef5385fa49a866e254bc9efd6ca0971ca213b5/lib/twilio-ruby/rest/api/v2010/account.rb#L679

For that reason, this does not work:

client = Twilio::REST::Client.new(account_sid, auth_token)
account = client.accounts.get(subaccount_sid)
number = account.messaging.v1.services.create(friendly_name: "Test Subaccount") # NoMethodError: undefined method `messaging' for #<Twilio::REST::Api::V2010::AccountInstance:0x00005617df452e30>

messaging is not the only missing namespace, as trusthub cannot be found either.

Technical details:

rafaeelaudibert commented 1 year ago

I'm trying to escalate this through your internal support ID #12597290. 2 weeks without any triage here sounds bad.

rafaeelaudibert commented 1 year ago

Twilio's support directed me to https://www.twilio.com/docs/iam/api/subaccounts#authentication, more specifically

Your main account credentials will allow you to access resources for any of your subaccounts that fall under the v2010 REST API. However, resources on subdomains, such as studio.twilio.com and taskrouter.twilio.com, must be accessed directly using subaccount credentials (API Keys or subaccount SID + subaccount auth token).

There are many ways to determine if a resource lives on its own subdomain or under v2010. One example method is to find the API Reference page for the resource, such as the Workspace resource. Observe the returned url value in the example payloads: if the URL begins with https://SOME-SUBDOMAIN.twilio.com, then you must access that resource using subaccount-specific credentials. If you do the same for some other resource, such as the Messaging resource, and see that uri begins with /2010-04-01, then it can be safely accessed using either your main account or subaccount credentials.


I'm still checking to see if there's the option to change this in the future to make the experience more seamless.

rafaeelaudibert commented 1 year ago

This was their response:

I confirmed with our Product Team, and there are no plans in the near future to implement this functionality. Using parent resources to do CRUD actions on subaccounts is something we actively suggest against.

I'll keep this issue open for now, but any Twilio rep can close it if you think this should be closed.