如何通过PowerShell列出subreddit的热门链接?

时间:2017-06-09 03:58:17

标签: powershell

在PowerShell中通过API检索subreddit链接的好方法是什么?

3 个答案:

答案 0 :(得分:2)

这是一种不依赖于任何第三方库的方法。即这是纯粹的PowerShell。

假设您在文件中保留了reddit凭据,reddit应用客户端ID和reddit应用客户端密码:

.execute("document.getElementsByClassName('price')[0].setAttribute('value', '19.99')")

(有关获取reddit客户端ID的信息,请参阅this page。)

检索访问令牌:

$username          = Get-Content $env:APPDATA\reddit-username
$password          = Get-Content $env:APPDATA\reddit-password

$app_client_id     = Get-Content $env:APPDATA\reddit-app-client-id
$app_client_secret = Get-Content $env:APPDATA\reddit-app-client-secret

让我们从结果中提取令牌并将其分配给变量:

$result_access_token = Invoke-RestMethod -Uri 'https://www.reddit.com/api/v1/access_token' `
    -Method Post `
    -Headers @{ 
        Authorization = ('Basic {0}' -f (
            [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(
                "${app_client_id}:${app_client_secret}"))))
    } `
    -Body @{ 
        grant_type = 'password'
        username   = $username
        password   = $password
    }

现在让我们在$token = $result_access_token.access_token 上获取当前的热门链接:

/r/programming

显示链接:

$result_listing = Invoke-RestMethod -Uri 'https://oauth.reddit.com/r/programming/hot' -Headers @{ Authorization = "bearer $token" }

示例输出:

enter image description here

答案 1 :(得分:2)

如果您将.json放在URI的末尾,Reddit会通过JSON提供内容,而PowerShell可以使用它。 / r /编程的热门链接:

(irm https://www.reddit.com/r/programming/hot/.json).data.children.data | select score, subreddit, title, url

答案 2 :(得分:1)

使用PSRAW

的示例
  1. 登录Reddit
  2. 登录Reddit
  3. 导航至https://ssl.reddit.com/prefs/apps
  4. 点击创建应用或创建其他应用按钮
  5. 输入名称(例如“(Reddit用户名)的PSRAW应用程序”)
  6. 选择脚本单选按钮
  7. 输入说明(例如“我的第一个PSRAW应用程序!”)
  8. 在about url框中输入https://127.0.0.1
  9. 在重定向URI框中输入https://127.0.0.1
  10. 点击创建应用按钮
  11. 然后定义您的Reddit应用程序,获取令牌并导出它:

    Install-Module -Name PSRAW -Scope CurrentUser
    Import-Module PSRAW
    
    $ClientCredential = Get-Credential
    $UserCredential = Get-Credential
    $RedirectUri = 'https://127.0.0.1'
    $AppExportPath = 'C:\PSRAW\MyApp.xml'
    $UserAgent = 'windows:markekraus-PSRAW:v0.0.0.1 (by /u/markekraus)'
    
    $Scopes = 'read'
    $Params = @{
        Script           = $True
        Name             = "markekraus's PSRAW App"
        Description      = 'My first PSRAW App!'
        ClientCredential = $ClientCredential
        UserCredential   = $UserCredential
        RedirectUri      = $RedirectUri
        UserAgent        = $UserAgent 
        Scope            = $Scopes
    }
    $RedditApp = New-RedditApplication @Params
    $RedditApp | Export-RedditApplication -Path $AppExportPath
    
    $TokenExportPath = 'C:\PSRAW\MyToken.xml'
    
    $Token = $RedditApp | Request-RedditOAuthToken -Script
    $Token | Export-RedditOAuthToken -Path $TokenExportPath
    

    现在,您可以使用Invoke-RedditRequest进行经过身份验证的API调用:

    $Uri = 'https://oauth.reddit.com/r/programming/hot'
    $result_listing = $Token | Invoke-RedditRequest -Uri $Uri
    $result_listing.ContentObject.data.children.data | 
        Select-Object score, subreddit, title, url
    

    首次创建应用程序,获取令牌并导出令牌后,可以将此类内容添加到您的配置文件中,以便在会话加载时显示:

    $TokenExportPath = 'C:\PSRAW\MyToken.xml'
    $Uri = 'https://oauth.reddit.com/r/programming/hot'
    
    $Token = Import-RedditOAuthToken -Path $TokenExportPath
    $result_listing = $Token | Invoke-RedditRequest -Uri $Uri
    $result_listing.ContentObject.data.children.data | 
        Select-Object score, subreddit, title, url