I am trying get mailbox folders using global admin account credentials via EWS. I am using the following ruby script with viewpoint gem to do it.
require 'viewpoint'
include Viewpoint::EWS
endpoint = 'https://outlook.office365.com/ews/exchange.asmx'
admin_email = '[email protected]'
password = 'password'
user_email = "[email protected]"
cli = Viewpoint::EWSClient.new endpoint, admin_email, password
cli.set_impersonation 'PrimarySmtpAddress', user_email #impersonation
act_as_hash = { act_as: user_email }
results = cli.folders(act_as_hash)
puts results.count
I have created required permissions and impersonations for all the mailboxes. I am having a issue when accessing shared mailboxes. I can access user mailbox folders using the same script.
When I tried the above script to get shared mailbox folders I am getting following error.
/Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/connection.rb:121:in `check_response': SOAP Error: Message: The primary SMTP address must be specified when referencing a mailbox. Code: a:ErrorNonPrimarySmtpAddress (Viewpoint::EWS::Errors::SoapResponseError)
from /Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/connection.rb:103:in `post'
from /Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/connection.rb:81:in `dispatch'
from /Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/soap/exchange_web_service.rb:212:in `do_soap_request'
from /Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/soap/exchange_data_services.rb:503:in `find_folder'
from /Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/folder_accessors.rb:45:in `folders'
from o365_test_connection.rb:14:in `<main>'
I tried to comment the impersonation line and use delegation to access the mailbox folders but I get the following error.
/Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/folder_accessors.rb:167:in `find_folders_parser': Could not retrieve folders. ErrorNonExistentMailbox: No mailbox with such guid. (Viewpoint::EWS::EwsFolderNotFound)
from /Users/yasitha/.rvm/gems/ruby-2.3.0/gems/viewpoint-1.1.0/lib/ews/folder_accessors.rb:46:in `folders'
from o365_test_connection.rb:14:in `<main>'
Can anyone help me on this?
Finally found the root cause of this error. Usually shared mailboxes don't require any licenses. Could not retrieve folders. ErrorNonExistentMailbox: No mailbox with such guid
error happens when we going to access unlicensed mailboxes.
I was able to access the mailbox when I try UserPrincipalName
instead of PrimarySmtpAddress
.