Confluence REST API通过`Invoke-RestMethod`未返回内容,但通过Web浏览器可见。

huangapple go评论53阅读模式
英文:

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://&lt;confluence server&gt;/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 &gt; $uri = &quot;https://&lt;confluence server&gt;/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName&quot;
PS &gt; 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://&lt;confluence server&gt;/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName; base=https://&lt;confluence server&gt;/confluence; context=/confluence}

I've also passed the output to a variable ($results) and checked the results property; it is empty:

PS &gt; $results = Invoke-RestMethod -Uri $uri -Credential $credential -Method Get
PS &gt; [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&amp;start=25&amp;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 &gt; $uri = &quot;https://&lt;confluence server&gt;/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName&quot;
PS &gt; $Headers = @{
    Authorization = &quot;Basic {0}&quot; -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(&quot;username:password&quot;))
}
PS &gt; 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://&lt;confluence server&gt;/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName; next=/rest/api/content/search?limit=25&amp;start=25&amp;cql=type=page+and+space=SpaceName; base=https://&lt;confluence server&gt;/confluence; context=/confluence}

Somehow, Invoke-RestMethod isn't passing the PSCredential credentials correctly with the -Credential parameter.

huangapple
  • 本文由 发表于 2023年3月21日 03:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定