AGSJSONRequestOperation with AGSCredential

2941
3
12-05-2013 09:53 AM
AllenBrunson
New Contributor
I am building an iOS app with the ArcGIS 10.2 SDK. I've gotten a lot of stuff implemented over the last few months. This part is stumping me, though.

Our ArcGIS server has some protected endpoints I want to get to with AGSJSONRequestOperation. So I create one of those, and attach an AGSCredential object to it, which is using AGSAuthenticationTypeToken. Alas, the server always returns an error, telling me that my "token is invalid or expired."

I've successfully used AGSJSONRequestOperation before, for unprotected endpoints. I've also successfully used AGSCredential objects, for logging in, doing queries with AGSQueryTask, getting information with AGSGeoprocessor, and so on, all with the AGSAuthenticationTypeToken auth type. But I am not able to use these two objects together.

Do any of your example projects access protected endpoints with AGSJSONRequestOperation? If so, I'm sure I could follow that.
0 Kudos
3 Replies
DiveshGoyal
Esri Regular Contributor
Allen,

You're on the right track. Things should work just as you describe by setting a credential on the request operation.
I wonder if any of the other objects (tasks, geoprocessor, etc) in your app are interfering with the request op. Are they also connecting to the same server using similar credentials? If so, can you try reusing that same credential object by setting it on the request op, instead of creating a new one.

Another option is to try and tweak the credential cache of the request op. Try using the default credential cache and if that doesn't work), try alloc/initing a new cache and setting it on the request op.

Let me know how things work out.

_
Divesh
0 Kudos
EricIto
New Contributor III
Can you post the code that is instantiating that request operation and assigning the credential (omitting any usernames/passwords)?
0 Kudos
YueWu1
by
Regular Contributor

I know this is a super old thread, however recently I run into an case with this issue. Just in case, if anyone else still curious about how to use AGSJSONRequestOperation to consume a secured service and return JSON back. Here I shared with a snippet of code in swift:

let cred = AGSCredential(user: "user1", password: "user1")

let queryParam = ["bbox": "-241.20512466608835,-27.80850864596666,51.688012394341115,67.48206676752923", "f":"pjson"]

let dataUrl = NSURL(string: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire_secure/MapServer")

let op = AGSJSONRequestOperation(URL: dataUrl, resource: "export", queryParameters: queryParam)

        op.credential = cred!

        op.completionHandler = { (obj:AnyObject!) -> Void in

        print("completed return with this JSON: '\(obj)'")

        }

       

        op.errorHandler = { (err:NSError!) -> Void in

        print(err)

        }

        AGSRequestOperation.sharedOperationQueue().addOperation(op)

    }

Hope this can help.

0 Kudos