【General】Using user token for HTML Post

This time, I will use an expression in [Sharperlight](https://www.sharperlight.com/) **Query Builder** to execute the defined scheduler task in advance.
The function in the expression I use is `HTML Post Web Page Content with authorization header`.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/a9c3615e-3d96-37b1-f896-efea30f77b14.png)
For using it, I have to generate a token used for Authrization Header first.

## Generating a token ##
Start [Sharperlight](https://www.sharperlight.com/) **Site Setup** and generate a token with Admin user who has  Administrator right.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/4f5477fe-b8e2-b492-933b-3b9d9121e4f4.png)
Select Admin user and show right-click menu, and select `Client Secret / Feed Key` -> `Create`
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/016db753-5c9a-7f7e-a657-4716dead9c2c.png)
you will see the `Client Secret / Feed Key` dialog with a generated token.
Type description and set expirly date
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/653695da-6993-993f-d523-381dbb801fe1.png)
You can copy the generated token to clipboard by clicking the button.
Then click OK to save it.

Keep the copied token somewhere for a while.

## Defining a published report ##
Start Sharperlight **Publisher** and click `New` for creating a new published query.

Fill in `Group`, `Code` and `Title`, and click `Edit Query` button to start **Query Builder** 
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/6c0cdab3-ddcc-e8a4-34fb-02d2eacb09c9.png) 
This query will be for just triggering a scheduled task so I choose **System** for `Product`, **User Accounts** for `Table` like this.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/78ebd57d-9653-739c-f1a6-e1d1b5c2fb5a.png)
On **Outputs** area, show right-click menu and choose `Add Expression`
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/53d37757-03a0-20ac-4aad-7a3b0b935707.png)
With the Expression dialog, type **post** in the `Find` field.
Choose `HTML Post Web Page Content with authorization header`
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/b8f8940f-ccec-b2be-afd1-313c534cbfd4.png)
By double-clicking it, its template will be populated into `Expression Formula` field.
The function requires 3 parameters, i.e. **Target Endpoint Url**, **Posting Data** and **Authorization Header**.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/e4d44d29-5008-72dc-55f6-15c82249dcef.png)
I want to trigger a schedule task so it should be like this.
Specify the token as the third parameter
```
UrlContentPost( "{_System.Rest.URL}SchedulerTaskPost"
, "&task=QiitaStat.PostedItemsGet&response=json"
"SLCS_34346537333..................2633234375aZ"
)
```
or spevify the token onto the body
```
UrlContentPost( "{_System.Rest.URL}SchedulerTaskPost"
, "&task=QiitaStat.PostedItemsGet&response=json"
+ "&userToken=SLCS_34346537333..................2633234375aZ"
,""
)
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/a5289ff2-33cd-4a65-8bd6-7f9a30bbf29e.png)
1. The endpoint URL to trigger scheduled task. `{_System.Rest.URL}` control tag will be translated with the proper sharperlight service Url in the runtime.
2. Specify the target task's `group` and `code`, and specify the token generated above.
3. set blank for the 3rd parameter.

Click `OK` to save it.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/763796c6-5098-ef48-b970-8f69026aeb69.png)
Rename the description as `Trigger Task`.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/9e213e12-ea9c-f570-16a4-6ca01f5f1c97.png)
## Testing the query ##
By clicking `Preview` button I can test the query. The trigger will be executed when I click `Preview` button this time.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/bf5d07f3-5292-8ca3-4fee-5dccad4f76ce.png)
The result is like this.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/1919a6bb-a8e1-95dd-ec11-947167d1b876.png)
Let's check the task was executed fine or not.
Start Sharperlight **Scheduler** and open the target task.
Go for History tab and check the log.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/8b0106bb-e154-647c-0ff0-d543476e2313.png)
It looks working ok.

Save the query by clicking `OK` button, and save the published query as well.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/94c42783-de9d-479b-e81c-26387926658b.png)

Whenever this report (Published Query) is opened with a browser, the specified task will be executed.
```
https://{your domain name}/{your sharperlight service code}/Report/?query=QiitaSample.PostWithToken   
```