Attachment URL with Token

1328
12
03-03-2023 04:18 PM
DavidPuckett
Occasional Contributor


I have used the example in the attachments sample to 'manually' construct a url for an attachment

For unsecured services\attachments, I can set an Image source to this url successfully.
E.g.

const attachmentUrl = featureLayerUrl + "/" + oid + "/attachments/" + attachment.attachmentId

However, for secured services, constructing the url in this way and then appending the Credential token does not work.
E.g.

const attachmentUrl = featureLayerUrl + "/" + oid + "/attachments/" + attachment.attachmentId + "?token=" + app.portal.credential.token

The image status reports an Image.Error

onStatusChanged: if (thumbnail.status === Image.Error) workRecordThumbnail.source = "../assets/image-error.png"

The portal credential works to access the services (feature layers) but it seems I cannot simply append the credential token to the attachment url to view the attachment.

I am creating the Credential like this:

Credential {
  id: portalCredential
  oAuthClientInfo: OAuthClientInfo {
    oAuthMode: Enums.OAuthModeUser
    clientId: app.clientId
  }
}


Any help?

0 Kudos
12 Replies
ZachBray
New Contributor II

Have you tried including the f query parameter to your attachmentURL?

const attachmentUrl = featureLayerUrl + "/" + oid + "/attachments/" + attachment.attachmentId + "?f=json&token=" + app.portal.credential.token

 

0 Kudos
DavidPuckett
Occasional Contributor

Thanks for the idea but it didn't resolve the issue. Still can't get to the attachment.

0 Kudos
ZachBray
New Contributor II

I ran the following through Postman and got the image back.  Where 209 is the Feature ObjectId and 144 is the attachment objectid from the attachments info endpoint, attachments.  I had content type set to application/json

https://stingray.svec.coop/server/rest/services/SVEC/Workorker_GPS/FeatureServer/0/209/attachments/1...

0 Kudos
ZachBray
New Contributor II

Forgot to mention, I added the w query parameter as referenced in the REST API docs.

https://developers.arcgis.com/rest/services-reference/enterprise/attachment-feature-service-.htm

 

0 Kudos
DavidPuckett
Occasional Contributor

@ZachBraythank you for this. So did you have an issue with using the url with a token as the source for an image? But using a Post worked for you? If so, I would be curious how you set the image source to the response from the Post request.

0 Kudos
JamesBallard1
Esri Regular Contributor

Hi @DavidPuckett,

Using the online map viewer and checking the network traffic, it looks like your url format is correct.

This is for OID=1, which only has one attachment (text file seen below).

Screen Shot 2023-03-06 at 3.28.56 PM.png

Once you're logged in and have the token, if it's not working in the app I'd copy/paste that into the browser and see if that works.

0 Kudos
DavidPuckett
Occasional Contributor

Pasting the url with the token from my app doesn't work either. I get an error from Web Adaptor saying 'Could not access any server machines. Please contact you system administrator'. So I guess the question is, why doesn't the token generated from the credential when a named user signs in work to get access to the attachments?

0 Kudos
JamesBallard1
Esri Regular Contributor

@DavidPuckett I am not sure, unfortunately. There may be some additional level of complexity in your situation compared to what I've tested. If @ZachBray 's suggestions don't help, I recommend reaching out to support so we can further help you.

0 Kudos
DavidPuckett
Occasional Contributor

I suspect you are right @JamesBallard1 We have a complex federated enterprise portal in place... thank you for the input.

I am curious if others have successfully used the attachment url with a token to display image attachments that are in secure portal environments however. Like, is this *supposed* to work ?! :collision:😎

0 Kudos