英文:
Confluence REST API content not returning results with `Invoke-RestMethod`, but results are visible via web browser
问题
我有一个Confluence站点,想通过Confluence REST API提取关于页面的信息。我使用CQL查询:cql=type=page and space=SpaceName
,所以我的URI是这样的:
https://<confluence服务器>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName
我可以从Web浏览器登录到Confluence,然后导航到上述URL,看到所有以JSON格式呈现的内容。
然而,当我使用完全相同的凭据和URI运行Invoke-RestMethod
时,它是成功的,但结果为0:
PS > $uri = "https://<confluence服务器>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName"
PS > Invoke-RestMethod -Uri $uri -Credential $credential -Method Get
我还将输出传递给变量($results
)并检查results
属性;它为空:
PS > $results = Invoke-RestMethod -Uri $uri -Credential $credential -Method Get
PS > [string]::IsNullOrEmpty($results)
True
我不知道问题是Confluence服务器行为不正常,我的凭据还是Invoke-RestMethod
。可能Confluence服务器通过Web浏览器允许查询/提供不同的结果,但在PowerShell中却不行吗?
有人知道这里发生了什么吗?
英文:
I have a Confluence site and I want to pull information about the pages via the Confluence REST API. I'm using a CQL query: cql=type=page and space=SpaceName
, so my URI is this:
https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName
I can log into Confluence from a web browser, then navigate to the above URL and I see all the content in JSON format.
However, when I run Invoke-RestMethod
using the exact same credentials and exact same URI, it's successful but there are 0 results:
PS > $uri = "https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName"
PS > Invoke-RestMethod -Uri $uri -Credential $credential -Method Get
results : {}
start : 0
limit : 25
size : 0
cqlQuery : type=page and space=SpaceName
searchDuration : 20
totalSize : 0
_links : @{self=https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName; base=https://<confluence server>/confluence; context=/confluence}
I've also passed the output to a variable ($results
) and checked the results
property; it is empty:
PS > $results = Invoke-RestMethod -Uri $uri -Credential $credential -Method Get
PS > [string]::IsNullOrEmpty($results)
True
I don't know if the issue is with the Confluence server not acting right, my credentials, or Invoke-RestMethod
. Is it possible that the Confluence server could be permitting queries/providing different results via a web browser but somehow not via PowerShell?
Anyone know what's going on here?
答案1
得分: 1
明白了。它正在正常运行。我之前使用-Credential
参数与PSCredential
(在过去的Bitbucket、Confiforms、Jira等工作中曾经有效)一起使用。当我切换到使用Basic Auth的-Headers
参数时,它开始按预期工作:
PS > $uri = "https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName"
PS > $Headers = @{
Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username:password"))
}
PS > Invoke-RestMethod -Uri $uri -Headers $Headers -Method Get
results : {@{id=514598038; type=page; status=current; title=page-title | 20230126-093349 | b05f41c; restrictions=; _links=; _expandable=}, @{id=514428061; type=page; status=current;...}
start : 0
limit : 25
size : 25
cqlQuery : type=page and space=SpaceName
searchDuration : 36
totalSize : 6642
_links : @{self=https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName; next=/rest/api/content/search?limit=25&start=25&cql=type=page+and+space=SpaceName; base=https://<confluence server>/confluence; context=/confluence}
不知何故,Invoke-RestMethod
使用-Credential
参数未正确传递PSCredential
凭据。
英文:
Got it working. I was using the -Credential
param with PSCredential
(which has worked for me in the past with Bitbucket, Confiforms, Jira, etc.). When I switched to the -Headers
parameter with Basic Auth, it started working as expected:
PS > $uri = "https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName"
PS > $Headers = @{
Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username:password"))
}
PS > Invoke-RestMethod -Uri $uri -Headers $Headers -Method Get
results : {@{id=514598038; type=page; status=current; title=page-title | 20230126-093349 | b05f41c; restrictions=; _links=; _expandable=}, @{id=514428061; type=page; status=current;...}
start : 0
limit : 25
size : 25
cqlQuery : type=page and space=SpaceName
searchDuration : 36
totalSize : 6642
_links : @{self=https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName; next=/rest/api/content/search?limit=25&start=25&cql=type=page+and+space=SpaceName; base=https://<confluence server>/confluence; context=/confluence}
Somehow, Invoke-RestMethod
isn't passing the PSCredential
credentials correctly with the -Credential
parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论