0

I'm trying to get a token for ArcGIS Online using C# (well, more specifically SSIS). I've got a URL I can use which works well, and I can hit it and get what I need in Chrome, but it won't work through C# using a WebClient.

I can get other JSONs through it through SSIS/C# WebClient, but NOT ArcGIS Online for some reason. The URL works through Chrome, but won't yield an answer through SSIS/C# WebClient.

My code (with client id/secret/credentials) is:

string webResource = "https://mymaps.maps.arcgis.com/sharing/rest/oauth2/token?client_id=MYCLIENTID&client_secret=MYCLIENTSECRET&grant_type=client_credentials";
WebClient wc = new WebClient();
string json = wc.DownloadString(webResource);

I would then parse the JSON.

Currently I'm getting:

DTS Script Task has encountered an exception in user code:
project name: ST_0a306cf2e1634509bea49b75b7d93265
Exception has been thrown by the target of an invocation.
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

I have created an application, as described in https://developers.arcgis.com/labs/rest/get-an-access-token/

An example of the returned JSON (through Chrome) is:

{"access_token":"blahblahblahcrazywords..","expires_in":7200}
  • 1
    For security, the HTTPS request should use a POST, not a GET, with the body URL-encoded. – Vince Oct 28 '19 at 10:16
  • @Vince thanks, this definitely helped. A lot was my own inexperience to be honest. – user25730 Oct 31 '19 at 3:18
0

Right then, this was three things combined.

As @Vince pointed out, it needed to be a POST, not a GET.

Also, limitations of using C# in an SSIS script task, and my lack of experience.

Also, Esri expects the URLs to be using security when from an application.

As such, I needed to use this asynchronously, and encode it differently:

public void Main()
{
    Dts.Variables["User::Token"].Value = GetToken().GetAwaiter().GetResult();
}

static async Task<string> GetToken()
        {
            try
            {
                var client = new System.Net.Http.HttpClient();
                HttpContent content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("client_id", "ValueHere"),
                    new KeyValuePair<string, string>("client_secret", "ValueHere"),
                    new KeyValuePair<string, string>("grant_type", "client_credentials")
                });
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                var response = await client.PostAsync("https://blahblahblah.maps.arcgis.com/sharing/rest/oauth2/token", content);
                var responseString = await response.Content.ReadAsStringAsync();
                return responseString;
            }
            catch (Exception exception)
            {
                return exception.ToString();
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.