javascript - 反应本机获取发布请求不起作用
问题描述
根据 Instagram API,这是一个获取access_token
.
curl -F 'client_id=XXXXXX' \
-F 'client_secret=XXXXXX' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=XXXXXX' \
-F 'code=XXXXXX' \
https://api.instagram.com/oauth/access_token
如果我在终端中运行它,我可以获得如下所示的结果:
{
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
"user": {
"id": "1574083",
"username": "snoopdogg",
"full_name": "Snoop Dogg",
"profile_picture": "..."
}
}
问题是它不适用于 react-native。
我正在使用此代码在 react-native 上发出发布请求:
fetch('https://api.instagram.com/oauth/access_token', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: 'XXXXXX',
client_secret: 'XXXXXX',
grant_type: 'authorization_code',
redirect_uri: 'XXXXXX',
code: 'XXXXXX'
}),
})
.then(res => res.json())
.then(obj => {
console.error(obj);
})
.catch((error) => {
console.error(error);
})
我不断得到回应You must provide a client_id.
我试图为这个请求创建一个可以工作的 jQuery 代码。
$.ajax({
type: "POST",
url: "https://api.instagram.com/oauth/access_token",
data: {
client_id: 'XXXXXX',
client_secret: 'XXXXXX',
grant_type: 'authorization_code',
redirect_uri: 'XXXXXX',
code: 'XXXXXX'
},
success: function(html) {
console.error(html)
},
error: function(html) {
console.error(html)
}
});
一个也可以工作的单行红宝石代码
Net::HTTP.post_form(URI.parse('https://api.instagram.com/oauth/access_token'), {'client_id': 'XXXXXX','client_secret': 'XXXXXX','grant_type': 'authorization_code','redirect_uri': 'XXXXXX','code': 'XXXXXX'}).body
你能看出我的代码有什么问题吗?它确实适用于 jQuery。
解决方案
推荐阅读
- vim - 在 VIM 中可视选择和拆分多行至 textwidth
- java - 在 Android Studio 中创建一个计算器。但我希望计算在一个文本视图中,并将结果发布在结果文本视图中
- python - 尝试发出正常运行时间命令(在上午 12 点之后转换为 -1 天)python
- reactjs - Redux - 在 TextInput 组件中输入内容后状态不会正确更新
- android - 如何处理 SQLiteDatabase 中 SELECT 查询语句中无法识别的字符?
- flutter - 在颤动中将第一个小部件居中
- javascript - 我想隐藏如果未指定自定义字段,则“softdown”部分
- kubernetes - Kubernetes,Helm - 指定配置时 helm 升级失败 - JupyterHub
- python - 在 IDE 中如何正确关闭 Python 程序?
- postgresql - PostGIS - 寻找空间和时间的交叉点