
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-29-2023 04:42 AM
In this article, today i will show you how to upload the file in ServiceNow from third party using Attachment API and Oauth token based Authorization.
First we need to create Oauth Application Registry in ServiceNow.
System Oauth -> Application Registries - > New
We want 3rd party application to upload file in ServiceNow, so we need to provide Oauth API end point for external clients. So select the option "Create an OAuth API endpoint for external clients".
You will see a form with the client key auto generated. Give a name and save. The client secret key will be generated automatically. Once it is saved, you shall see something like this.
To upload a file into ServiceNow, we already have an OOTB API from ServiceNow which is Attachment API. Let us make use of that API.
On left side, if you see there are 2 ways in which you can upload the file. One thorugh binary request and another in the form of upload. Depending on the 3rd party application, you can provide whichever API they need.
In this example, I will show you how you can upload a file into servicenow from another servicenow instance using this API. For this, i will using the binary request file upload.
Source is SN1 isntance.
Destination is SN2 instance.
In SN2 instance, we have created OAuth application registry with option "OAuth API end point for external clients". Then we nee dto generate the token for the 1st time using the credentials and share this with the SN1 isntance.
To generate token, here is how you can do it. Go to postman and create a new request. The endpoint is your instance/oauth_token.do and then pass the parameters as you see below.
Capture the response, and you need to give this response to SN1 instance. The SN1 instance will use this access_token to connect to your instance. And if the access token is expired, then they have to regenerate the token using the refresh token that we have received above.
End Point: https://your-instance.service-now.com/oauth_token.do
HTTP Method: POST
Body:
x-www-form-urlencoded:
grant_type refresh_token //send this as is.
client_id YOUR_CLIENTID
client_secret YOUR_CLIENT_SECRET
refresh_token YOUR_REFRESH_TOKEN //you will find this from the initial oauth token response.
So along with he initial oatuh token response, you need to give your client_id and client_secret key for them to generate the token. This way, you do not need to pass the credentials and using access token they can connect to your instnace.
Once they are able to generate the token, then they can connect to our servicenow Attachment API and can upload the file in the provided record. Here is sample code where we are reading the file from SN1 insance and then uploading into SN2 instance.
//CODE TO READ THE CONTENT OF ATTACHMENT IN SN1 INSTANCE.
//before this code, you need to fetch the attachment sys_id which you wanted to upload into the other instance.
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://sn1instance.service-now.com/api/now/attachment/YOUR_ATTACHMENT_SYSID/file');
request.setHttpMethod('GET');
request.setRequestHeader("Accept","*/*");
var response = request.execute();
var content =response.getBody(); //This returns the content of the attachment.
gs.log(ResponseBody);
//Then call the SN2 intance end point to upload the file there.
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://sn2instance.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=INCIDENT_SYSID&file_name=your_filename');
request.setHttpMethod('POST');
//Here you need to pass the token that we have sent like below.
//Bearer + <access token>
request.setRequestHeader("Authorization","Bearer oybjnx3XkA77wgDy4x0ZIHJf3guz-B3FNy72J7PlIMHwwgPSiyfYHPM-rt9RXbZWVMQ_k-olmdZ9dhOhxwsllQ");
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/doc'); //Pass the appropriate content type based on the file you are sending.
request.setRequestBody(content); //pass the content here
var response = request.execute();
gs.log(response.getBody());
Let me know if you have any questions in the comments below.
Mark the article as helpful and bookmark if you found it useful.
- 7,135 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
the content type must be different its either binary or multipart