Select to view content in your preferred language

ESRI web map: How to pass the OAuth token to fetch the web map without passing userid

7206
5
12-26-2013 09:19 PM
MaiRong
Emerging Contributor
I'm using ESRI javascript API 3.7 for web map service. The web map is based on Oauth2 authentication. I use java code to fetch the token. Then I need to fetch the map in javascript by calling esri.arcgis.utils.createMap(). But How to pass the token with this request? I know ESRI3.7 offers a API called esri.IdentityManager.registerToken() that can pass the token will the request. But this method requires the user name as parameter. I don't want to put the username in the javascript code, as it's not safe. Are there any other ways to pass the token with the request? The api for esri.IdentityManager.registerToken() is here: https://developers.arcgis.com/en/jav...#registertoken Thanks a lot.
:confused: Is it a must to send the username with token to query authenticated web map? Is there a way to get the result only with the token?
0 Kudos
5 Replies
RichardWatson
Deactivated User
I am not familiar with the JavaScript API but I have used the REST API itself.

You might try passing the token as a URL parameter, i.e. &token=#####.  That is how you pass it in REST.
0 Kudos
MaiRong
Emerging Contributor
I am not familiar with the JavaScript API but I have used the REST API itself.

You might try passing the token as a URL parameter, i.e. &token=#####.  That is how you pass it in REST.


In fact I've tried it before. I appended the token to the URL to fetch web map like this:

https://www.arcgis.com/sharing/rest/content/items/{webmapId}/data?f=json&token={token}

Then I got a json object as following:

{
    "baseMap": {
        "baseMapLayers": [
            {
                "id": "World_Street_Map_1271",
                "opacity": 1,
                "url": "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer",
                "visibility": true
            }
        ],
        "title": "World_Street_Map"
    },
    "operationalLayers": [
        {
            "id": "USA_States_Generalized_9320",
            "layerDefinition": {
                "drawingInfo": {
                    "renderer": {
                        "symbol": {
                            "color": [
                                255,
                                214,
                                180,
                                0
                            ],
                            "outline": {
                                "color": [
                                    251,
                                    164,
                                    93,
                                    255
                                ],
                                "style": "esriSLSSolid",
                                "type": "esriSLS",
                                "width": 0
                            },
                            "style": "esriSFSSolid",
                            "type": "esriSFS"
                        },
                        "type": "simple"
                    }
                }
            },
            "mode": 1,
            "opacity": 1,
            "popupInfo": {
                "description": null,
                "fieldInfos": [
                    {
                        "fieldName": "FID",
                        "format": {
                            "digitSeparator": true,
                            "places": 0
                        },
                        "isEditable": true,
                        "label": "FID",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "STATE_NAME",
                        "format": null,
                        "isEditable": true,
                        "label": "STATE_NAME",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "SUB_REGION",
                        "format": null,
                        "isEditable": true,
                        "label": "SUB_REGION",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "STATE_ABBR",
                        "format": null,
                        "isEditable": true,
                        "label": "STATE_ABBR",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "POP2010",
                        "format": {
                            "digitSeparator": true,
                            "places": 0
                        },
                        "isEditable": true,
                        "label": "POP2010",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "POP10_SQMI",
                        "format": {
                            "digitSeparator": true,
                            "places": 2
                        },
                        "isEditable": true,
                        "label": "POP10_SQMI",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "POP2012",
                        "format": {
                            "digitSeparator": true,
                            "places": 0
                        },
                        "isEditable": true,
                        "label": "POP2012",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "POP12_SQMI",
                        "format": {
                            "digitSeparator": true,
                            "places": 2
                        },
                        "isEditable": true,
                        "label": "POP12_SQMI",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                    {
                        "fieldName": "WHITE",
                        "format": {
                            "digitSeparator": true,
                            "places": 0
                        },
                        "isEditable": true,
                        "label": "WHITE",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    },
                 // omit some other fieldNames to shorten the text
                    ,
                    {
                        "fieldName": "SQMI",
                        "format": {
                            "digitSeparator": true,
                            "places": 2
                        },
                        "isEditable": true,
                        "label": "SQMI",
                        "stringFieldOption": "textbox",
                        "tooltip": "",
                        "visible": true
                    }
                ],
                "mediaInfos": [],
                "showAttachments": true,
                "title": "USA_States_Generalized: "
            },
            "title": "USA_States_Generalized - USA_States_Generalized",
            "url": "http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_States_Generalized/FeatureServer/0",
            "visibility": false
        }
    ],
    "version": "1.9.1"
}


However, we expected to get an object that contains "map" property to add some manipulations on the map. I don't know what this returned json is used for. It seems to be a definition of the web map.
0 Kudos
RichardWatson
Deactivated User
It looks to me like your token worked fine.

Here is the documentation on the REST API for Item:

http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000000ms000000.htm

It looks to me like it is doing what it was designed to do.  I think that your question is which API and arguments do you use to do the thing that you want to do.  Other than studying the API, one trick that I sometimes use is to run Fiddler and then use some ESRI tool which does what I want, e.g. ArcMap, and then look at the HTTP traffic in order to understand the API and arguments that it is using.  For example, can you edit the web map using ArcGIS Online tools?  If so then use Fiddler to see what it is doing under the covers.

Best of luck!
0 Kudos
MaiRong
Emerging Contributor
It looks to me like your token worked fine.

Here is the documentation on the REST API for Item:

http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000000ms000000.htm

It looks to me like it is doing what it was designed to do.  I think that your question is which API and arguments do you use to do the thing that you want to do.  Other than studying the API, one trick that I sometimes use is to run Fiddler and then use some ESRI tool which does what I want, e.g. ArcMap, and then look at the HTTP traffic in order to understand the API and arguments that it is using.  For example, can you edit the web map using ArcGIS Online tools?  If so then use Fiddler to see what it is doing under the covers.

Best of luck!


Thanks a lot. 🙂
0 Kudos
IanKramer3
Deactivated User
Hi,
So I have a solution in this posting towards the top. 

http://forums.arcgis.com/threads/87617-Bypass-user-login-dialog-and-automatically-signing-a-user-acc...

You need to get the OAuth token first, and then register it with the kernel like this (full solution at URL above):

var token = {
"server": "http://www.arcgis.com/sharing/rest",
"userId": userId,
"token": tokenStr,
"ssl": false,
"expires": expireStr
};

kernel.id.registerToken(token);


I believe that the register process stores the token as a cookie on the client, similar to how it works when you manually log into ArcGIS.com. 

After that, you can call the create webmap function.  You don't need to pass anything to that function, since you already registered your token. 

Ian
0 Kudos