古い OAuth アクセストークンの有効期限が切れると、新しい OAuth アクセストークンを自動的に取得します (サポートされていないカスタムスケジュール済みジョブ)Summary<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } span { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } h2 { font-size: 24pt; font-family: Lato; color: var(--now-color--text-primary, black); } h3 { font-size: 18pt; font-family: Lato; color: var(--now-color--text-primary, black); } h4 { font-size: 14pt; font-family: Lato; color: var(--now-color--text-primary, black); } a { font-size: 12pt; font-family: Lato; color: var(--now-color--link-primary, #00718F); } a:hover { font-size: 12pt; color: var(--now-color--link-primary, #024F69); } a:target { font-size: 12pt; color: var(--now-color--link-primary, #032D42); } a:visited { font-size: 12pt; color: var(--now-color--link-primary, #00718f); } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } 以下のスクリプトは、基本的に新しいアクセストークンを取得するための「refresh_token」権限許可タイプを使用していることに注意してください。次のスクリプトを使用して、Scheduled Script Execution で、期限切れが近づいているアクセストークンを自動的に取得できます。 (リフレッシュトークンがまだ期限切れになっていない場合) この例は、OAuth で構成された送信 REST メッセージ用です。 これは参照用のサンプルスクリプトであり、サポートされていないカスタマイズとしてカウントされることに注意してください。さらにカスタマイズを行う場合は、OAuth クライアントライブラリーの ServiceNow ドキュメントを参照してください。 重要:ServiceNow プラットフォームは、以前のアクセストークンの有効期限が切れている場合、かつ有効なリフレッシュトークンがoauth_credentialテーブルに存在する場合にのみ、新しいアクセストークンをフェッチできることに注意してください。お客様は、新しいアクセストークンをフェッチするためにこのスケジュール済みジョブを書き込む必要はありません。 また、トークンプロバイダーがアクセストークンの取得中に新しいリフレッシュトークンを送信できる場合、プラットフォームはoauth_credentialテーブルで新しく受信したリフレッシュトークンを更新できます。 Release<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } span { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } h2 { font-size: 24pt; font-family: Lato; color: var(--now-color--text-primary, black); } h3 { font-size: 18pt; font-family: Lato; color: var(--now-color--text-primary, black); } h4 { font-size: 14pt; font-family: Lato; color: var(--now-color--text-primary, black); } a { font-size: 12pt; font-family: Lato; color: var(--now-color--link-primary, #00718F); } a:hover { font-size: 12pt; color: var(--now-color--link-primary, #024F69); } a:target { font-size: 12pt; color: var(--now-color--link-primary, #032D42); } a:visited { font-size: 12pt; color: var(--now-color--link-primary, #00718f); } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } Instructions<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } span { font-size: 12pt; font-family: Lato; color: var(--now-color--text-primary, #000000); } h2 { font-size: 24pt; font-family: Lato; color: var(--now-color--text-primary, black); } h3 { font-size: 18pt; font-family: Lato; color: var(--now-color--text-primary, black); } h4 { font-size: 14pt; font-family: Lato; color: var(--now-color--text-primary, black); } a { font-size: 12pt; font-family: Lato; color: var(--now-color--link-primary, #00718F); } a:hover { font-size: 12pt; color: var(--now-color--link-primary, #024F69); } a:target { font-size: 12pt; color: var(--now-color--link-primary, #032D42); } a:visited { font-size: 12pt; color: var(--now-color--link-primary, #00718f); } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: ; max-width: ; width: ; height: ; } } function refreshAccessToken(requestorId, oauthProfileId, token) { if ( !(token && requestorId && oauthProfileId)) return; var tokenRequest = new sn_auth.GlideOAuthClientRequest(); tokenRequest.setGrantType("refresh_token"); tokenRequest.setRefreshToken(token.getRefreshToken()); tokenRequest.setParameter('oauth_requestor_context','sys_rest_message'); tokenRequest.setParameter('oauth_requestor', requestorId); tokenRequest.setParameter('oauth_provider_profile',oauthProfileId); var oAuthClient = new sn_auth.GlideOAuthClient(); var tokenResponse = oAuthClient.requestTokenByRequest(null,tokenRequest); var error = tokenResponse.getErrorMessage(); if (error) gs.warn("Error:" + tokenResponse.getErrorMessage()); } function isExpired(expiresIn, withinSeconds) { if (expiresIn > withinSeconds) return false; return true; } function getToken(requestorId, oauthProfileId) { if (!requestorId || !oauthProfileId) return null; var client = new sn_auth.GlideOAuthClient(); return client.getToken(requestorId, oauthProfileId); } function checkAndRefreshAccessToken(grRestMessage) { if (grRestMessage.getValue("authentication_type") != "oauth2" ) return false; var accountMsg = grRestMessage.getValue("name"); if (!accountMsg) accountMsg = grRestMessage.getUniqueValue(); accountMsg = "Account=\"" + accountMsg + "\""; var token = getToken(grRestMessage.getUniqueValue(), grRestMessage.getValue('oauth2_profile')); var accessToken = token.getAccessToken(); if (accessToken) { if (!isExpired(token.getExpiresIn(), 300)) return; } if (!token.getRefreshToken()) { gs.error("No OAuth refresh token for Rest Message. Manual reauthorization required. " + accountMsg); return; } if (isExpired(token.getRefreshTokenExpiresIn(), 0)) { gs.error("OAuth refresh token for Rest Message is expired. Manual reauthorization required. " + accountMsg); return; } gs.info("Refreshing oauth access token for Rest Message account. " + accountMsg); refreshAccessToken(grRestMessage.getUniqueValue(), grRestMessage.getValue('oauth2_profile'), token);} var grAccount = new GlideRecord("sys_rest_message");grAccount.addQuery("authentication_type", "oauth2");grAccount.addNotNullQuery("oauth2_profile");grAccount.query(); while (grAccount.next()) { checkAndRefreshAccessToken(grAccount);}