swift - Office 365 iOS SDK - How to invoke SharePoint REST API -
all ios sdk samples provide working code accessing mail, calendar, odfb files, none show how access sharepoint list items. trying simple rest call in swift, keep getting following error:
[0] (null) @"error_description" : @"unsupported security token.
here subset of code when app starts:
var resourceid : string = "https://mytenant.sharepoint.com" var authorityurl : string = "https://login.windows.net/common/" var clientid : string = "xxd4200eb-7284-41be-a434-abb269b82f0f" var redirecturi : nsurl = nsurl(string: "http://www.mycompanywebsite.com")! override func viewdidload() { super.viewdidload() var defaults: nsuserdefaults = nsuserdefaults.standarduserdefaults() var er : adauthenticationerror? = nil var authcontext:adauthenticationcontext = adauthenticationcontext(authority: authorityurl, error: &er) authcontext.acquiretokenwithresource(resourceid, clientid: clientid, redirecturi: redirecturi) { (result: adauthenticationresult!) -> void in if (result.accesstoken == nil) { println("token nil") } else { defaults.setobject(result.accesstoken, forkey: "accesstokendefault") defaults.synchronize() println("accesstoken: \(result.accesstoken)") } } }
then, once token, invoke following code tries http fails:
var resolver : msodatadefaultdependencyresolver = msodatadefaultdependencyresolver() var credentials : msodataoauthcredentials = msodataoauthcredentials() var defaults: nsuserdefaults = nsuserdefaults.standarduserdefaults() credentials.addtoken(defaults.objectforkey("accesstokendefault") as! string) var credentialsimpl : msodatacredentialsimpl = msodatacredentialsimpl() credentialsimpl.setcredentials(credentials) resolver.setcredentialsfactory(credentialsimpl) //build api string sample list info let request = nsmutableurlrequest(url: nsurl(string: "https://umaknow.sharepoint.com/_api/web?$select=title")!) request.httpmethod = "get" let token = defaults.stringforkey("accesstokendefault") request.setvalue("bearer \(token)", forhttpheaderfield: "authorization") request.setvalue("application/json; odata=verbose", forhttpheaderfield: "accept") //make call sharepoint rest api nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue(), completionhandler:{ (response:nsurlresponse!, data: nsdata!, error: nserror!) -> void in var error:nserror? = nil let jsonresult: nsdictionary! = nsjsonserialization.jsonobjectwithdata(data, options:nsjsonreadingoptions.mutablecontainers, error: &error) as? nsdictionary if (jsonresult != nil) { //parse json file objects in table view let results:nsarray = (jsonresult["d"] as! nsdictionary)["results"] as! nsarray
and fails error message. monitoring web traffic, following bit more details going on:
this request (raw):
get /_api/web?$select=title http/1.1 host: mytenant.sharepoint.com connection: keep-alive proxy-connection: keep-alive accept: application/json; odata=verbose user-agent: o365demo/1 cfnetwork/711.3.18 darwin/14.3.0 accept-language: en-us authorization: bearer optional("eyj0exaioijkv1qildjhbgcioijsuzi1niising1dci6ik1uq19wwmnbvgznnxbpwwlkse1iytlnb0vlwsisimtpzci6ik1uq19wwmnbvgznnxbpwwlkse1iytlnb0vlwsj9.eyjhdwqioijodhrwczovl3vtywtub3cuc2hhcmvwb2ludc5jb20ilcjpc3mioijodhrwczovl3n0cy53aw5kb3dzlm5ldc81nmzjotc3oc04ywfjltq1zditotmwns1iote3mwzmywzhogmviiwiawf0ijoxndm1mji3nzk1lcjuymyioje0mzuymjc3otusimv4cci6mtqzntizmty5nswidmvyijoims4wiiwidglkijointzmyzk3nzgtogfhyy00nwqyltkzmdutyjkxnzfmzmfmythjiiwib2lkijoioguyztblzdqtymeznc00ywm4ltkwymmtnwq3ngq3mze4yjkyiiwidxbuijoicgllcnjlqhvtywtub3cuy29tiiwichvpzci6ijewmdmwmdawoduymuy3njyilcjzdwiioijqr3bhq1vdddnytnm2b0pjskgxvldqouwyv1jla3liohhxwhlkbvfasv8wiiwiz2l2zw5fbmftzsi6ilbpzxjyzsisimzhbwlsev9uyw1lijoiqm91cmfzc2eilcjuyw1lijoiugllcnjliejvdxjhc3nhiiwiyw1yijpbinb3zcjdlcj1bmlxdwvfbmftzsi6inbpzxjyzub1bwfrbm93lmnvbsisimfwcglkijoinmq0mjawzwitnzi4nc00mwjllwe0mzqtywjimjy5yjgyzjbmiiwiyxbwawrhy3iioiiwiiwic2nwijoiqwxsu2l0zxmutwfuywdliefsbfnpdgvzlljlywqgq2fszw5kyxjzlljlywrxcml0zsbgawxlcy5szwfkiedyb3vwlljlywquqwxsiedyb3vwlljlywrxcml0zs5bbgwgtxlgawxlcy5szwfkie15rmlszxmuv3jpdgugu2l0zxmuumvhzc5bbgwgu2l0zxmuu2vhcmnolkfsbcbuzxjtu3rvcmuuumvhzc5bbgwgvxnlci5szwfkifvzzxiuumvhzfdyaxrlifvzzxiuumvhzfdyaxrllkfsbcisimfjcii6ijeifq.aakaeifuoeii0zrydzaobtl5wyqldybhfvbsj6nzak4jqkbzf6bhjsaanhu9qj8omr2gudvr3vcngzefvlzxcf3u0k6r8g4176m-bu3raabri9djyazj24jms1u-kl0h5ee8mvnxsi7bf7qv9joehiixlei_sxba1s8mhdwmaw9se9tl8mbbfplddblxua4ygc_rywo7g7rw3jee3gmev9nffz7zklxxd55p8fxtbz0-khi0wbrhixn69wauc0jiqhj4fccgzlvtuuubirhurrhi4uizyplwqqnr0i8zwamvr8wuxcwtzhpkzoz5teqbvbwp1uwyui42o6s0pfykzq") accept-encoding: gzip, deflate
and raw response site
http/1.1 401 unauthorized server: microsoft-iis/8.5 x-ms-diagnostics: 3000006;reason="token contains invalid signature.";category="invalid_client" sprequestguid: 84c9139d-807c-2000-0e59-4caf75bd097f request-id: 84c9139d-807c-2000-0e59-4caf75bd097f sprequestduration: 19 spiislatency: 1 x-powered-by: asp.net microsoftsharepointteamservices: 16.0.0.4107 x-content-type-options: nosniff x-ms-invokeapp: 1; requirereadonly p3p: cp="all ind dsp cor adm cono cur cuso ivao ivdo psa psd tai telo our samo cnt com int nav onl phy pre pur uni" www-authenticate: bearer realm="56fc9778-8aac-45d2-9305-b9171ffafa8c",client_id="00000003-0000-0ff1-ce00-000000000000",trusted_issuers="00000001-0000-0000-c000-000000000000@*,https://sts.windows.net/*/,00000003-0000-0ff1-ce00-000000000000@90140122-8516-11e1-8eff-49304924019b",authorization_uri="https://login.windows.net/common/oauth2/authorize" date: thu, 25 jun 2015 11:12:40 gmt content-length: 51 {"error_description":"unsupported security token."}
so there wrong way use token provided, limited oauth2 knowledge , lack of samples, @ lost.
any appreciated!
in case gets same issue, found wrong. has nothing adal or sharepoint rest, syntactic error in swift. line read:
request.setvalue("bearer \(token)", forhttpheaderfield: "authorization")
generates following in http request:
authorization: bearer optional("eyj0exaioi....
the "optional("...") has insertion of token variable in string. replacing statement with:
request.setvalue("bearer " + token!, forhttpheaderfield: "authorization")
now generates correct header in http request:
authorization: bearer eyj0exaioi...
and data want call.
so newbie swift programmer error! :-)
Comments
Post a Comment