簡體   English   中英

嘗試使用Javascript從Reddit獲取圖像?

[英]Trying to get images from Reddit using Javascript?

對於此特定任務,我決定最好使用Reddit API。 在查看可用的不同包裝器時,我選擇使用snoowrap 這些示例非常清楚,我想使用類似的方式進行身份驗證:

const snoowrap = require('snoowrap');

const otherRequester = new snoowrap({
    userAgent: 'put your user-agent string here',
    clientId: 'put your client id here',
    clientSecret: 'put your client secret here',
    username: 'put your username here',
    password: 'put your password here'
});

我可以在Reddit Apps(“首選項”部分)上找到重要信息,例如clientIDclientSecret 令我感到困惑的是userAgent輸入。 我應該在這里輸入什么?

我以為可以轉到由同一用戶創建的Reddit OAuth幫助器 但是,在過程結束時,我似乎收到了Reddit Bad Request

用戶代理是從以下位置發送請求的瀏覽器:

您可以使用以下命令填充該數據:

navigator.userAgent

編輯:

上面的方法僅在客戶端起作用,在服務器端,如果您處於Nodejs(Expressjs)環境中,則可以從執行API調用的函數的request參數中的標頭數據中獲取用戶代理。 像這樣:

app.get('/api-call', function(request, response){
    const snoowrap = require('snoowrap');

    const otherRequester = new snoowrap({
        userAgent: request.headers['user-agent'], 
        clientId: 'put your client id here',
        clientSecret: 'put your client secret here',
        username: 'put your username here',
        password: 'put your password here'
    });
    // rest of the code
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM