2017-06-09 45 views

回答

2

這是一種不依賴任何第三方庫的方法。即它是純粹的PowerShell。

讓我們假設你保持你的reddit的憑據,reddit的應用程序客戶端ID和Reddit應用程序客戶端祕密文件:

$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 

(約得到一個reddit的客戶ID信息,請參閱this page

檢索一個訪問令牌:

$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" } 

顯示的鏈接:

$result_listing.data.children | ForEach-Object data | Select-Object score, subreddit, title, url 

輸出示例:

enter image description here

2

如果您將.json放在URI的末尾,則Reddit可以通過JSON提供內容,PowerShell可以使用該內容。在/ R /編程熱門鏈接:

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

使用PSRAW

  1. 登錄到reddit的
  2. 登錄到reddit的
  3. 導航到https://ssl.reddit.com/prefs/apps
  4. 單擊創建應用程序或創建一個應用按鈕
  5. 輸入名稱(例如「(Reddit用戶名)的PSRAW應用程序」)
  6. 選擇腳本單選按鈕
  7. 輸入描述(例如, 「我的第一個PSRAW應用程序!「)
  8. 在約網址框中輸入https://127.0.0.1
  9. 在重定向的URI框中輸入https://127.0.0.1
  10. 點擊創建應用程序按鈕

然後定義你的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