簡體   English   中英

在NodeJS中使用Mailchimp API執行OAuth2

[英]Execute OAuth2 with the Mailchimp API in NodeJS

這個問題可能會暫時禁止我訪問質量較低的問題,但是無論如何,這里都是這樣。

我一直在關注MailChimp的OAuth2教程( https://developer.mailchimp.com/documentation/mailchimp/guides/how-to-use-oauth2/ ),但是在使用代碼和secret / key參數進行發布請求時陷入困境。 我得到的最接近的是400錯誤請求響應。 我的代碼:

var args = { headers: { "Content-Type": "application/json" }, body: "grant_type=authorization_code&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + encoded_url + "&code=" + req.query.code, url: "https://login.mailchimp.com/oauth2/token" }; var r = request.post(args, function(err, req, body){ console.log(err) console.log(req) })

任何幫助或教程都很好。 谷歌搜索已經很久了,但還沒有變得更近。

好的,我遇到了同樣的事情,終於找到了。 我的代碼與您的代碼非常相似:

this.globals.request({
        method: "POST",
        uri: "https://login.mailchimp.com/oauth2/token",
        form:  {
            grant_type: "authorization_code",
            client_id: this.globals.mailchimp_client_id,
            client_secret: this.globals.mailchimp_client_secret,
            redirect_uri: encodeURIComponent(fullUrl),
            code: req.query.code
        }
    },
    function (error, response, body) {
        if (error || response.statusCode !== 200) {
            self.globals.logger.debug(self.moduleName, "getMailchimpToken", "Error " + response.statusCode + "(" + response.statusMessage + ") from mailchimp: " + error, true);
            body = {};
        }
        self.globals.logger.debug(self.moduleName, "getMailchimpToken", "got body: " + JSON.stringify(body), false);
        deferred.resolve(body);
});

我懷疑您的問題是,您在表單數據中使用了編碼的URL,但是請求將再次對其進行編碼。 當我查看查詢字符串時,我看到類似

&redirect_uri=http%253A%252F%252F

%s被重新編碼的位置。 encodeURIComponent(fullUrl),更改為fullUrl ,並且可以正常工作。

需要注意的一個重要事項-redirect_uri應該與OAuth調用中使用的相同, 包括path 請注意,根據Mailchimp文檔,您可以從注冊應用程序時使用的redirect_url更改實際調用中的路徑,這在這一點上很重要。

暫無
暫無

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

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