node.js - 豆瓣oauth调用出错?
大家讲道理
大家讲道理 2017-04-17 11:13:46
[Node.js讨论组]

用nodejs的superagent客户端来调用豆瓣oauth,要获取的access_token的时候老出现javascript required_parameter_is_missing: client_id的问题。有人遇到这种问题吗?豆瓣上面有好多人问,但是都解决不了我的问题:

我的代码:

    exports.user_info = function (code) {
    return request.post(config.douban.authorize_token_url)
        .send("client_id", config.douban.app_id)
        .send("client_secret", config.douban.app_secret)
        .send("redirect_uri", config.back.code_back_url)
        .send("grant_type", "authorization_code")
        .send("code", code)
        .set("ContentType", "application/x-www-form-urlencoded")
        .end(function (res) {
            console.log(res.text);
            return request.get(config.douban.user_info_url + res.body.douban_user_id)
                .set("Authorization", "Bearer " + res.body.access_token)
                .end(function (response) {
                    console.log(response.text);
                    return response;
                });
        });
};

错误类似豆瓣上的问题,http://www.douban.com/group/topic/40590418/。再把app_id贴出来大家帮着看看:05a8eec5691e6dcf10facc503b5b496d。由于code可以正确得到所有app_id肯定没有问题。

按照楼下的建议,我用python试了一下:

>>> url = token_url + urllib.urlencode(params)
>>> url
'https://www.douban.com/service/auth2/tokencode=dbf492bfa678692e&client_secret=ec0faf30f04562a6&redirect_uri=https%3A%2F%2Fwww.douban.com%2Fservice%2Fauth2%2Ftoken&client_id=05a8eec5691e6dcf10facc503b5b496d&grant_type=authorization_code'
>>> url = token_url + "?"+urllib.urlencode(params)
>>> url
'https://www.douban.com/service/auth2/token?code=dbf492bfa678692e&client_secret=XXXXXXXXX&redirect_uri=https%3A%2F%2Fwww.douban.com%2Fservice%2Fauth2%2Ftoken&client_id=05a8eec5691e6dcf10facc503b5b496d&grant_type=authorization_code'
>>> from requests import *
>>> post(url)
<Response [400]>
>>> post(url).text
u'{"msg":"required_parameter_is_missing: client_id","code":113,"request":"POST \\/auth2\\/token"}'
>>> 

豆瓣上面好像说也可以把参数放在url后面所以我就试了一下,问题依旧啊。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(3)
阿神

所有value需要urlencode

ringa_lee

https://www.douban.com/service/auth2/tokencode=dbf492bfa难道不应该是https://www.douban.com/service/auth2/?tokencode=dbf492bfa吗?


update:

还有,为啥post提交要把参数作为GET方式过去……

迷茫

setRequestHeader("Content-Type","application/x-www-form-urlencoded");

是Content-Type,不是ContentType

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号