Token authentication in 100.0.0

1575
5
03-16-2017 11:35 AM
DavidMomper
New Contributor II

When I try to make calls to a token-secured RemoteResource using the new 100.0.0 API, I get an exception:
"com.esri.arcgisruntime.io.JsonEmbeddedException: Token Required"

Is there a way to add a Token to a UserCredentials object? In the 10.2.4, I would create a UserCredentials object, and simply call setUserToken(), but that method no longer exists. In the new API, the UserCredentials class only takes a username and a password. There is another type of Credential, OAuthTokenCredential, but this is not appropriate to my use case because I'm not using OAuth.

How is non-OAuth token-based authentication handled in the new API?

0 Kudos
5 Replies
nita14
by
Occasional Contributor III

David,

Look at the snippet below.

ArcGISMapImageLayer trafficLayer = new ArcGISMapImageLayer(url);
trafficLayer.setId("TrafficLayer");
trafficLayer.setCredential(new UserCredential("ArcGISOnlineUserNameCaseSensitive", "password"));

Regards,

Adam

0 Kudos
DavidMomper
New Contributor II

That's not what I'm looking for. That's just username and password. I need a way to handle token-based authentication, as described here: ArcGIS Security and Authentication | ArcGIS for Developers 

Token-based: Your app provides a valid user name and password for the user. In the response, you receive a token that is included with requests for secured content on the portal for authenticated resources. Available with ArcGIS Online and ArcGIS Enterprise version 10.2 or earlier.

The UserCredential class can only store a username and password, not a token. Specifically what I'm looking for is:

1. How to connect to a token generation service to obtain a token? Is there anything like OAuthTokenCredentialRequest that will work for non-OAuth token generation?

2. Once I have a valid token, how to use the token to authenticate with a RemoteResource? Maybe a subclass of Credential that can store a token, so I can return it from my AuthenticationChallengeHandler.

0 Kudos
nita14
by
Occasional Contributor III

David,

ok, I see what you are getting at. Well, I am not aware of such possibility in ArcGIS Runitme 100.0.0.

You can generate valid token at the ArcGIS Online service endpoint , but you need to somehow handle its refreshing and appending to ArcGIS Server endpoint requests, which in fact UserCredentials do for you.

Regards,

Adm

0 Kudos
DavidMomper
New Contributor II

Adam Nicinski wrote:

You can generate valid token at the ArcGIS Online service endpoint , but you need to somehow handle its refreshing and appending to ArcGIS Server endpoint requests, which in fact UserCredentials do for you.

Can you please elaborate on how UserCredentials does this? I can't find any way to get a UserCredentials to handle tokens; if you know a way to do this, can you please post a few lines of code to demonstrate how it works?

0 Kudos
nita14
by
Occasional Contributor III

David,

What I want to say is just that UserCredentials creates and refershes tokens automatically for you on the valid generateToken endpoint. 

I can only quote the docs:

Class Overview

Represents a named user credential. User credentials will be used to authenticate against HTTP-secured services, and to generate tokens for authenticating token-secured services.

Hope this helps,

Adam

0 Kudos