Home

Could not access delegated/impersonated shared mailbox via EWS

%3CLINGO-SUB%20id%3D%22lingo-sub-158292%22%20slang%3D%22en-US%22%3ECould%20not%20access%20delegated%2Fimpersonated%20shared%20mailbox%20via%20EWS%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-158292%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22post-text%22%3E%0A%3CP%3EI%20am%20trying%20get%20mailbox%20folders%20using%20global%20admin%20account%20credentials%20via%20EWS.%20I%20am%20using%20the%20following%20ruby%20script%20with%20viewpoint%20gem%20to%20do%20it.%3C%2FP%3E%0A%3CPRE%20class%3D%22lang-rb%20prettyprint%20prettyprinted%22%3E%3CCODE%3E%3CSPAN%20class%3D%22pln%22%3Erequire%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E'viewpoint'%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0A%0Ainclude%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EViewpoint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EEWS%0A%0Aendpoint%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E'https%3A%2F%2Foutlook.office365.com%2Fews%2Fexchange.asmx'%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0Aadmin_email%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E'admin%40hostname.com'%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0Apassword%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E'password'%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0A%0Auser_email%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22user%40hostname.com%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0A%0Acli%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22typ%22%3EViewpoint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EEWSClient%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Enew%20endpoint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20admin_email%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20password%0Acli%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eset_impersonation%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E'PrimarySmtpAddress'%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20user_email%20%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22com%22%3E%23impersonation%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0Aact_as_hash%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20act_as%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20user_email%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%7D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0Aresults%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20cli%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Efolders%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eact_as_hash%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0A%0Aputs%20results%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ecount%3C%2FSPAN%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EI%20have%20created%20required%20permissions%20and%20impersonations%20for%20all%20the%20mailboxes.%20I%20am%20having%20a%20issue%20when%20accessing%20shared%20mailboxes.%20I%20can%20access%20user%20mailbox%20folders%20using%20the%20same%20script.%3C%2FP%3E%0A%3CP%3EWhen%20I%20tried%20the%20above%20script%20to%20get%20shared%20mailbox%20folders%20I%20am%20getting%20following%20error.%3C%2FP%3E%0A%3CPRE%20class%3D%22lang-rb%20prettyprint%20prettyprinted%22%3E%3CCODE%3E%3CSPAN%20class%3D%22str%22%3E%2FUsers%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eyasitha%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ervm%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Egems%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eruby%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E2.3%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Egems%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eviewpoint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E1.1%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Elib%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eews%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Econnection%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Erb%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E121%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Ein%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%60check_response'%3A%20SOAP%20Error%3A%20Message%3A%20The%20primary%20SMTP%20address%20must%20be%20specified%20when%20referencing%20a%20mailbox.%20%20Code%3A%20a%3AErrorNonPrimarySmtpAddress%20(Viewpoint%3A%3AEWS%3A%3AErrors%3A%3ASoapResponseError)%0A%20%20%20%20from%20%2FUsers%2Fyasitha%2F.rvm%2Fgems%2Fruby-2.3.0%2Fgems%2Fviewpoint-1.1.0%2Flib%2Fews%2Fconnection.rb%3A103%3Ain%20%60%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Epost%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E'%0A%20%20%20%20from%20%2FUsers%2Fyasitha%2F.rvm%2Fgems%2Fruby-2.3.0%2Fgems%2Fviewpoint-1.1.0%2Flib%2Fews%2Fconnection.rb%3A81%3Ain%20%60dispatch'%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0A%20%20%20%20from%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EUsers%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eyasitha%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ervm%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Egems%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eruby%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E2.3%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Egems%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eviewpoint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E1.1%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Elib%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eews%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Esoap%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eexchange_web_service%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Erb%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E212%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Ein%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%60do_soap_request'%0A%20%20%20%20from%20%2FUsers%2Fyasitha%2F.rvm%2Fgems%2Fruby-2.3.0%2Fgems%2Fviewpoint-1.1.0%2Flib%2Fews%2Fsoap%2Fexchange_data_services.rb%3A503%3Ain%20%60%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Efind_folder%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E'%0A%20%20%20%20from%20%2FUsers%2Fyasitha%2F.rvm%2Fgems%2Fruby-2.3.0%2Fgems%2Fviewpoint-1.1.0%2Flib%2Fews%2Ffolder_accessors.rb%3A45%3Ain%20%60folders'%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%0A%20%20%20%20from%20o365_test_connection%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Erb%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E14%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Ein%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%60%3CMAIN%3E'%3C%2FMAIN%3E%3C%2FSPAN%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EI%20tried%20to%20comment%20the%20impersonation%20line%20and%20use%20delegation%20to%20access%20the%20mailbox%20folders%20but%20I%20get%20the%20following%20error.%3C%2FP%3E%0A%3CPRE%20class%3D%22lang-rb%20prettyprint%20prettyprinted%22%3E%3CCODE%3E%3CSPAN%20class%3D%22str%22%3E%2FUsers%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eyasitha%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ervm%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Egems%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eruby%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E2.3%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Egems%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eviewpoint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E1.1%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Elib%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eews%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2F%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Efolder_accessors%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Erb%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E167%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Ein%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%60find_folders_parser'%3A%20Could%20not%20retrieve%20folders.%20ErrorNonExistentMailbox%3A%20No%20mailbox%20with%20such%20guid.%20(Viewpoint%3A%3AEWS%3A%3AEwsFolderNotFound)%0A%20%20%20%20from%20%2FUsers%2Fyasitha%2F.rvm%2Fgems%2Fruby-2.3.0%2Fgems%2Fviewpoint-1.1.0%2Flib%2Fews%2Ffolder_accessors.rb%3A46%3Ain%20%60%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Efolders%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E'%0A%20%20%20%20from%20o365_test_connection.rb%3A14%3Ain%20%60%3CMAIN%3E'%3C%2FMAIN%3E%3C%2FSPAN%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ECan%20anyone%20help%20me%20on%20this%3F%3C%2FP%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-158292%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edelegate%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EEWS%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EImpersonation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
New Contributor

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 = 'admin@hostname.com'
password = 'password'

user_email = "user@hostname.com"

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?