本文介绍了如何使此 PowerShell Web 请求正确保存会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成功登录网站并执行查询的脚本:

$credential = Get-Credential$postParams = @{username=$credential.UserName;password=$credential.GetNetworkCredential().password}Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/sessions/?Login -Method POST -Body $postParams -SessionVariable cookie$query = Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/mods/searchresults/?-WebSession $cookie

我想做的是对另一个网站 Trello 做类似的事情.这是脚本:

$credential = Get-Credential$postParams = @{method='password';'factors[user]'=$credential.UserName;'factors[password]'=$credential.GetNetworkCredential().password}Invoke-WebRequest -Uri https://trello.com/1/authentication -Method POST -Body $postParams -SessionVariable cookie$result = Invoke-WebRequest -uri https://trello.com/-WebSession $cookie

然而,结果变量显示一个页面,就好像用户没有登录一样,所以我假设会话没有正确保存.我该如何解决这个问题?

解决方案

如果您想使用 Web 上的远程服务,最简单的方法是使用它们的 API.下面是如何走这条路.

在此处注册 API 的第 1 步:

登录并单击以授权此令牌,我们稍后需要在代码中使用它.

完成后,您将在控制台中看到这一点

>我们检索了一个代码,在 $code 中检查该值

此函数返回一个全局变量 $global:code,其中包含您在请求任何信息时需要提供的授权令牌.例如,要查看有关您的用户帐户的信息,最后一行使用 /members/me 端点存储有关您的用户帐户的信息.以下是此请求的完成方式:

$endpoint = "https://api.trello.com/1/members/me/boards"$auth = "?key=$trellokey&token=$global:code"$request = $endpoint + $auth调用 RESTMethod $request

您现在可以使用其目录中列出的任何 API 端点来执行您的任何操作想要使用 Trello.只需用您想要的信息的 URL 替换端点,然后就可以了.

这是与网络资源交互的一种受支持且标准的方式,因此您的努力将是值得的.

如果您确实通过网页自动化实现了这一点,那么您将受到 Trello 的支配.如果他们对页面进行了更改,您可能需要重写所有代码.

I have this script that successfully logs into a website and performs a query:

$credential = Get-Credential

$postParams = @{username=$credential.UserName;password=$credential.GetNetworkCredential().password}

Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/sessions/?Login -Method POST -Body $postParams -SessionVariable cookie

$query = Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/mods/searchresults/? -WebSession $cookie

What I am trying to do is do a similar thing with another website, Trello. This is the script:

$credential = Get-Credential

$postParams = @{method='password';'factors[user]'=$credential.UserName;'factors[password]'=$credential.GetNetworkCredential().password}

Invoke-WebRequest -Uri https://trello.com/1/authentication -Method POST -Body $postParams -SessionVariable cookie

$result = Invoke-WebRequest -uri https://trello.com/ -WebSession $cookie

However, result variable displays a page as if the user was not logged in, so I'm assuming the session isn't properly saving. How can I fix this?

解决方案

If you want to work with remote services on the web, the easiest way to do this is by using their API. So here's how to go that route.

Step 1 signup for the API here: https://trello.com/app-key

Copy down this key as your $trelloKey in the code below.

Step 2 Download this Show-OAuthWindow function which has been customized to work with Trello.

$trellokey ='redacted'

$r = Show-OAuthWindow "https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=$trellokey"

$me = invoke-restmethod "https://api.trello.com/1/members/me/boards?key=$trellokey&token=$global:code"

When you run this code, you'll get a login window. Sign in and then close the form.

Sign in and and click to authorize this token, which we need to use later in our code.

When this completes, you'll see this in the console

>we retrieved a code, check within $code for the value

This function returns a global variable of $global:code which contains the authorization token you need to provide when you request any info. For Example, to see information about your user account, the final line stores info about your user account, using the /members/me endpoint. Here's how this request is done:

$endpoint = "https://api.trello.com/1/members/me/boards"
$auth = "?key=$trellokey&token=$global:code"
$request = $endpoint + $auth
Invoke-RESTMethod $request

You can now use any of the API endpoints listed in their catalog to do whatever you'd like with Trello. Simply replace the endpoint with the URL for the info you want, and away you go.

This is a supported and standard way of interacting with web resources, so your efforts will be well spent.

If you DID get this working with web page automation, you would be at the mercy of Trello. If they made changes to their page, you might have to rewrite all of your code.

这篇关于如何使此 PowerShell Web 请求正确保存会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 17:44
查看更多