Skip to content

Commit a97489c

Browse files
committed
Adding Auto-AuthTokens
Adding auto AuthToken Refresh to avoid having to copy paste them into global variables.
1 parent bd0eb7f commit a97489c

6 files changed

+364
-64
lines changed

Collections/OneNote API.postman_collection.json

+115-29
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@
112112
"description": "Gets the notebooks for a list of onenote weburls"
113113
},
114114
"response": []
115+
},
116+
{
117+
"name": "CreateNewNotebook",
118+
"request": {
119+
"method": "GET",
120+
"header": [],
121+
"body": {
122+
"mode": "raw",
123+
"raw": ""
124+
},
125+
"url": {
126+
"raw": ""
127+
}
128+
},
129+
"response": []
115130
}
116131
],
117132
"description": "All available POST actions in Notebooks API",
@@ -653,7 +668,7 @@
653668
"bearer": [
654669
{
655670
"key": "token",
656-
"value": "{{AuthToken}}",
671+
"value": "{{{{AuthToken}}}}",
657672
"type": "string"
658673
}
659674
]
@@ -665,7 +680,72 @@
665680
"id": "4fcc87ca-73a0-4014-93cd-88c9089ecd59",
666681
"type": "text/javascript",
667682
"exec": [
668-
""
683+
"const AuthTokenName = \"AuthToken\";",
684+
"const AuthTokenExpName = \"AuthTokenExp\";",
685+
"const AuthTokenUrlName = \"AuthTokenUrl\";",
686+
"const ClientIdName = \"ClientId\";",
687+
"const passwordName = \"password\";",
688+
"const usernameName = \"username\";",
689+
"const resource = \"https://officeapps.live.com\";",
690+
"const scope = \"openid\";",
691+
"",
692+
"// Object used to request an authtoken",
693+
"// Based on currently selected postman environment",
694+
"const echoPostRequest = {",
695+
" url:getGlobalVarThroughEnvironmentVar(AuthTokenUrlName),",
696+
" method: 'POST',",
697+
" body: {",
698+
" mode: 'urlencoded',",
699+
" urlencoded: [",
700+
" { key: \"grant_type\", value: \"password\" },",
701+
" { key: \"username\", value: getGlobalVarThroughEnvironmentVar(usernameName)},",
702+
" { key: \"password\", value: getGlobalVarThroughEnvironmentVar(passwordName) },",
703+
" { key: \"resource\", value: resource },",
704+
" { key: \"scope\", value: scope },",
705+
" { key: \"client_id\", value: getGlobalVarThroughEnvironmentVar(ClientIdName)}",
706+
" ]",
707+
" }",
708+
"}",
709+
"",
710+
"// Verify if the AuthToken needs to be refreshed",
711+
"if (!getGlobalVarThroughEnvironmentVar(AuthTokenName) || ",
712+
" !getGlobalVarThroughEnvironmentVar(AuthTokenExpName)) {",
713+
" console.log('AuthToken or expiry date are missing')",
714+
" requestAuthToken();",
715+
"}",
716+
"else if (getGlobalVarThroughEnvironmentVar(AuthTokenExpName) <= (new Date()).getTime()) {",
717+
" console.log('AuthToken is expired')",
718+
" requestAuthToken();",
719+
"}",
720+
"else {",
721+
" console.log(\"Current AuthToken is still valid.\");",
722+
"}",
723+
"",
724+
"// Makes a request to bet a new AuthToken.",
725+
"// This will store the new auth token as a global variable.",
726+
"function requestAuthToken (){",
727+
" pm.sendRequest(echoPostRequest, function (err, res) {",
728+
" console.log(err ? err : res.json());",
729+
" if (err === null) {",
730+
" console.log('Saving the token and expiry date')",
731+
" var responseJson = res.json();",
732+
" setGlobalVarThroughEnvironmentVar(AuthTokenName, responseJson.access_token)",
733+
" ",
734+
" var expiryDate = new Date();",
735+
" expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in);",
736+
" setGlobalVarThroughEnvironmentVar(AuthTokenExpName, expiryDate.getTime());",
737+
" }",
738+
" });",
739+
"}",
740+
"",
741+
"// Using an env variable to grab the global variable allows us to avoid checking for what env we are running on here. Instead we rely on postman's current env selection to search for the right global variable. See how globals are setup (they are basically a collection of both prod and ppe environment variables that are selected based on the env we are running)",
742+
"function getGlobalVarThroughEnvironmentVar(varName){",
743+
" return pm.globals.get(pm.environment.get(varName));",
744+
"}",
745+
"",
746+
"function setGlobalVarThroughEnvironmentVar(varName, value){",
747+
" return pm.globals.set(pm.environment.get(varName),value);",
748+
"}"
669749
]
670750
}
671751
},
@@ -682,166 +762,172 @@
682762
],
683763
"variable": [
684764
{
685-
"id": "27dd41e1-9eb4-4bd1-a6b3-5951fe5c3c5b",
765+
"id": "944ea569-9ccb-4142-bafe-1f91d21966ec",
686766
"key": "AppIdHeader",
687767
"value": "MS-Int-AppId",
688768
"type": "string"
689769
},
690770
{
691-
"id": "bab30ef8-d149-4035-91cc-805066188bbd",
771+
"id": "a5c1724e-4999-43ed-b313-903cca95072a",
692772
"key": "AppIdHeader_Value",
693773
"value": "Testing",
694774
"type": "string"
695775
},
696776
{
697-
"id": "83cd5418-04cc-4599-96c1-ba5782fee84f",
777+
"id": "4974d457-4da1-41a5-af81-1831e596c16c",
698778
"key": "Me",
699779
"value": "{{Site}}/api/{{version}}/me",
700780
"type": "string"
701781
},
702782
{
703-
"id": "a5448c9f-6feb-4f28-a863-d2fa62bf1f0e",
783+
"id": "98d97588-53a7-47df-a421-2540f3392088",
704784
"key": "MeNotebooks",
705785
"value": "{{Me}}/notes/notebooks",
706786
"type": "string"
707787
},
708788
{
709-
"id": "09cb72f3-8378-4e8c-99ea-9e9a48b68d18",
789+
"id": "8a38cd0e-1221-4e30-bc41-4bdb76007981",
710790
"key": "User",
711791
"value": "{{Site}}/api/{{version}}/users('{{UserId}}')",
712792
"type": "string"
713793
},
714794
{
715-
"id": "3416584c-a122-46ff-adc2-118374d34d18",
795+
"id": "a84f3e15-d116-4895-8473-1dfb4af2c1f9",
716796
"key": "UserNotebooks",
717797
"value": "{{User}}/notes/notebooks",
718798
"type": "string"
719799
},
720800
{
721-
"id": "e4df61fa-296c-46b2-9c98-df0c2a4d0756",
801+
"id": "833b68bc-877e-4431-af63-063cb61a058b",
722802
"key": "MeClassNotebooks",
723803
"value": "{{Me}}/notes/classnotebooks",
724804
"type": "string"
725805
},
726806
{
727-
"id": "261df2b5-5218-4c35-9790-c60360a3c8bd",
807+
"id": "de23dabd-833f-45e0-acaf-a53a5052736d",
728808
"key": "MeSections",
729809
"value": "{{Me}}/notes/sections",
730810
"type": "string"
731811
},
732812
{
733-
"id": "749901cd-60f9-4a51-b6a3-ad5f2064fdc4",
813+
"id": "46e64f47-3f5b-4c0c-a02f-9165393e5f07",
734814
"key": "MeSectionGroups",
735815
"value": "{{Me}}/notes/sectiongroups",
736816
"type": "string"
737817
},
738818
{
739-
"id": "2c478448-f0da-4e04-bfa5-e6c3d20b0764",
819+
"id": "47ac2929-6a60-48df-89fb-a9b5ef8477c6",
740820
"key": "MeResources",
741821
"value": "{{Me}}/notes/resources",
742822
"type": "string"
743823
},
744824
{
745-
"id": "0e5ccb28-79ae-49a8-bdda-4ff7228de9bd",
825+
"id": "c97bf9e3-e619-4041-b05d-68e39c29e730",
746826
"key": "WopiHost",
747827
"value": "{{Site}}/host",
748828
"type": "string"
749829
},
750830
{
751-
"id": "07cc0245-c2d0-41df-aad5-400013bc496f",
831+
"id": "292036fb-4115-4346-b358-d2ed84285c68",
752832
"key": "WOPIEndpoint",
753833
"value": "{{Site}}/sync/{{WOPIVersion}}/Wopi",
754834
"type": "string"
755835
},
756836
{
757-
"id": "35e4cac5-5820-4e45-88dc-94daecdc4c1c",
837+
"id": "ce78324b-56f2-4721-bbae-ecf20b252ff3",
758838
"key": "MePages",
759839
"value": "{{Me}}/notes/pages",
760840
"type": "string"
761841
},
762842
{
763-
"id": "e950e3fd-449d-4b76-bedf-6a4fafceed10",
843+
"id": "ee2c2a0f-1a8d-4c3e-82a5-a86161dde602",
764844
"key": "Target_Body",
765845
"value": "body",
766846
"type": "string"
767847
},
768848
{
769-
"id": "cb458a39-1623-4638-805d-20bb6a14e1d2",
849+
"id": "e003e729-3bfe-4eca-8e9d-886e06b237b8",
770850
"key": "UserId",
771851
"value": "<Update current value only, leave this initial value as is>",
772852
"type": "string"
773853
},
774854
{
775-
"id": "f4fb182b-6464-4825-b475-d8d83025b594",
855+
"id": "2b6cd8eb-e3d4-4eef-ae3b-3d8dcd253b88",
776856
"key": "PageId",
777857
"value": "<Update current value only, leave this initial value as is>",
778858
"type": "string"
779859
},
780860
{
781-
"id": "a282b197-5e2c-4f27-8d68-d5bdeec7b2c6",
861+
"id": "b047d47d-c99a-44c5-935d-971b9a5fb87a",
782862
"key": "SectionId",
783863
"value": "<Update current value only, leave this initial value as is>",
784864
"type": "string"
785865
},
786866
{
787-
"id": "19011c28-0885-4481-b966-e7e043304cce",
867+
"id": "9ee943e5-60a1-44a5-a0c8-aafa8de86a27",
788868
"key": "SectionGroupId",
789869
"value": "<Update current value only, leave this initial value as is>",
790870
"type": "string"
791871
},
792872
{
793-
"id": "0dde8fa3-7db3-49f9-ad00-2b0746ab113a",
873+
"id": "97b5fc7d-886c-4bbb-a00e-052c08cf92d4",
794874
"key": "NotebookId",
795875
"value": "<Update current value only, leave this initial value as is>",
796876
"type": "string"
797877
},
798878
{
799-
"id": "295ad84f-8345-459d-83b5-52dc28cb6b4c",
879+
"id": "4c8bb370-0c98-4dad-9781-fe46b8626e12",
800880
"key": "ResourceId",
801881
"value": "<Update current value only, leave this initial value as is>",
802882
"type": "string"
803883
},
804884
{
805-
"id": "c66ce02b-d97d-49ec-a707-fdf3b7711220",
885+
"id": "9c29be8a-392d-444b-9207-6b02d6ff120c",
806886
"key": "MachineName",
807887
"value": "<Update current value only, leave this initial value as is>",
808888
"type": "string"
809889
},
810890
{
811-
"id": "e0df9ae4-0840-41ec-8397-e9a9a59d953d",
891+
"id": "8e81cc0a-8361-415f-877e-da7185c1037c",
812892
"key": "WebUrl",
813893
"value": "<Update current value only, leave this initial value as is>",
814894
"type": "string"
815895
},
816896
{
817-
"id": "144bc424-902e-474f-b239-0ac931dae5a1",
897+
"id": "2837b63e-520d-478c-a736-737ccb26f2ea",
818898
"key": "WebUrls",
819899
"value": "<Update current value only, leave this initial value as is>",
820900
"type": "string"
821901
},
822902
{
823-
"id": "d88b074f-b159-42c7-a904-b6e3fd351639",
903+
"id": "95dd85aa-9856-49f5-9a65-3942dd5b30f1",
824904
"key": "Target_Id",
825905
"value": "<Update current value only, leave this initial value as is>",
826906
"type": "string"
827907
},
828908
{
829-
"id": "1304283e-6fb6-4f28-ad6e-20009dc50a18",
909+
"id": "35c8ad64-c57d-4459-b84e-a99e7e630ba6",
830910
"key": "HTMLContent",
831911
"value": "<Update current value only, leave this initial value as is>",
832912
"type": "string"
833913
},
834914
{
835-
"id": "38061292-d589-4b22-b97d-e9874e1eb410",
915+
"id": "d24a5daa-1561-4583-8905-82ca4bb0df26",
836916
"key": "Test_Title",
837917
"value": "<Update current value only, leave this initial value as is>",
838918
"type": "string"
839919
},
840920
{
841-
"id": "ff04f9cb-d831-4f7e-904a-45e2dbf05016",
921+
"id": "38a5a4e4-c6aa-458a-9e08-35ae790e81ce",
842922
"key": "Test_JapaneseTitle",
843923
"value": "<Update current value only, leave this initial value as is>",
844924
"type": "string"
925+
},
926+
{
927+
"id": "d6df31e0-8442-431f-a219-f37b76983e53",
928+
"key": "NotebookName",
929+
"value": "<Update current value only, leave this initial value as is>",
930+
"type": "string"
845931
}
846932
]
847933
}

Environments/Edog.postman_environment.json

+47-7
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,70 @@
55
{
66
"key": "superdomain",
77
"value": "edog",
8-
"description": "",
8+
"description": {
9+
"content": "",
10+
"type": "text/plain"
11+
},
912
"enabled": true
1013
},
1114
{
1215
"key": "Site",
1316
"value": "https://{{superdomain}}.onenote.com",
14-
"description": "",
17+
"description": {
18+
"content": "",
19+
"type": "text/plain"
20+
},
1521
"enabled": true
1622
},
1723
{
1824
"key": "version",
1925
"value": "beta",
20-
"description": "",
26+
"description": {
27+
"content": "",
28+
"type": "text/plain"
29+
},
2130
"enabled": true
2231
},
2332
{
2433
"key": "AuthToken",
25-
"value": "{{PPE_AuthToken}}",
26-
"type": "text",
34+
"value": "PPE_AuthToken",
35+
"description": {
36+
"content": "",
37+
"type": "text/plain"
38+
},
39+
"enabled": true
40+
},
41+
{
42+
"key": "AuthTokenExp",
43+
"value": "PPE_AuthToken_Exp",
44+
"enabled": true
45+
},
46+
{
47+
"key": "AuthTokenUrl",
48+
"value": "PPE_AuthToken_Url",
49+
"description": "",
50+
"enabled": true
51+
},
52+
{
53+
"key": "ClientId",
54+
"value": "PPE_client_id",
55+
"description": "",
56+
"enabled": true
57+
},
58+
{
59+
"key": "username",
60+
"value": "PPE_UserName",
61+
"description": "",
62+
"enabled": true
63+
},
64+
{
65+
"key": "password",
66+
"value": "PPE_Password",
2767
"description": "",
2868
"enabled": true
2969
}
3070
],
3171
"_postman_variable_scope": "environment",
32-
"_postman_exported_at": "2018-11-01T01:45:01.846Z",
33-
"_postman_exported_using": "Postman/6.4.4"
72+
"_postman_exported_at": "2018-12-06T04:48:48.240Z",
73+
"_postman_exported_using": "Postman/6.5.3"
3474
}

0 commit comments

Comments
 (0)