Azure开发扩展:使用oauth2刷新服务端点令牌

hrysbysz  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(214)

我目前正在构建一个扩展,我添加了一个服务端点,用于从我们的服务器获取一些信息。
我们的服务器使用Azure AD进行身份验证,我成功授权DevOps中的服务连接,并且正确提取了信息。但是,使用的令牌在某个时间点过期,我想知道如何刷新它。
关于这个功能的文档有点缺乏,我有点迷路了
下面是清单json:

{
            "id": "service-endpoint",
            "description": "Service endpoint to get game information",
            "type": "ms.vss-endpoint.service-endpoint-type",
            "targets": [ "ms.vss-endpoint.endpoint-types" ],
            "properties": {
                "name": "portal",
                "displayName": "Portal Access",
                "dataSources": [
                  {
                    "name": "AccessToken",
                    "endpointUrl": "{{{configuration.Url}}}/token",
                    "requestVerb": "Post",
                    "requestContent": "grant_type=authorization_code&code={{{#uriDataEncode 1 AuthorizationCode}}}{{{/uridataencode}}}&client_id={{{#uriDataEncode 1 configuration.ClientId}}}{{{/uridataencode}}}&client_secret={{{#uriDataEncode 1 configuration.ClientSecret}}}{{{/uridataencode}}}&redirect_uri={{{#uriDataEncode 1 RedirectUrl}}}{{{/uridataencode}}}",
                    "resultSelector": "jsonpath:$",
                    "headers": [
                      {
                        "name": "Content-Type",
                        "value": "application/x-www-form-urlencoded"
                      }
                    ]
                  },
                  {
                    "name": "Game",
                    "endpointUrl": "{{{endpoint.url}}}/Studio/studios/games",
                    "requestVerb": "Get",
                    "resultSelector": "jsonpath:$.[*]",
                    "headers": [
                      {
                        "name": "Content-Type",
                        "value": "application/json"
                      }
                    ]
                  },
                  {
                    "name": "RefreshToken",
                    "endpointUrl": "{{{configuration.Url}}}/token",
                    "requestVerb": "Post",
                    "requestContent": "grant_type=refresh_token&refresh_token={{{#uriDataEncode 1 RefreshToken}}}{{{/uridataencode}}}&client_id={{{#uriDataEncode 1 configuration.ClientId}}}{{{/uridataencode}}}&client_secret={{{#uriDataEncode 1 configuration.ClientSecret}}}{{{/uridataencode}}}",
                    "resultSelector": "jsonpath:$",
                    "headers": [
                      {
                        "name": "Content-Type",
                        "value": "application/x-www-form-urlencoded"
                      }
                    ]
                  }
                ],
                "authenticationSchemes": [
                  {
                    "displayName": "i18n:OAuth2",
                    "type": "ms.vss-endpoint.endpoint-auth-scheme-oauth2",
                    "headers": [
                      {
                        "name": "Authorization",
                        "value": "Bearer {{{endpoint.AccessToken}}}"
                      }
                    ],
                    "authorizationUrl": "{{{configuration.Url}}}/authorize?client_id={{{configuration.ClientId}}}&response_type=code&redirect_uri={{{RedirectUrl}}}&scope=api://03105a38-d4dd-4fa1-8d6a-d1ef5c918574/API.Access",
                    "dataSourceBindings": [
                      {
                        "target": "AccessToken",
                        "dataSourceName": "AccessToken",
                        "resultTemplate": "{\"AccessToken\" : \"{{{access_token}}}\", \"RefreshToken\" : \"{{{refresh_token}}}\", \"ExpiresIn\" : \"{{{expires_in}}}\", \"TokenType\" : \"{{{token_type}}}\", \"Scope\" : \"{{{scope}}}\", \"Error\" : \"{{{error}}}\", \"ErrorDescription\" : \"{{{error_description}}}\"}"
                      },
                      {
                        "target": "RefreshToken",
                        "dataSourceName": "RefreshToken",
                        "resultTemplate": "{\"AccessToken\" : \"{{{access_token}}}\", \"RefreshToken\" : \"{{{refresh_token}}}\", \"ExpiresIn\" : \"{{{expires_in}}}\", \"TokenType\" : \"{{{token_type}}}\", \"Scope\" : \"{{{scope}}}\", \"Error\" : \"{{{error}}}\", \"ErrorDescription\" : \"{{{error_description}}}\"}"
                      }
                    ]
                  }
                ],
                "helpMarkDown": ""

提前感谢!

mspsb9vt

mspsb9vt1#

我尝试在我的环境中重现相同的结果,并得到以下结果:

请注意,要获取刷新标记,请添加**offline_access**API权限:

我使用以下参数生成了访问令牌

GET
https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
client_id:client_id
grant_type:authorization_code
code:code
redirect_uri:redirect_uri
code_verifier:S256
scope:499b84ac-1321-427f-aa17-267ca6975798/user_impersonation offline_access
client_secret:client_secret

回应:

要刷新访问令牌,请使用以下参数:

GET
https://login.microsoftonline.com/TenantID/oauth2/v2.0/token


client_id:client_id
grant_type:refresh_token
scope:scope
client_secret:client_secret
refresh_token:refreshtoken

访问令牌将按如下方式成功刷新:

相关问题