iT邦幫忙

0

linebot 獲取 user profile 的問題 (python)

各位高手好,小弟一直卡關,請版上各位高手解惑
我已經成功得到access_token,想透過access_token
獲取user profile,但一直不成功,錯誤顯示401

程式碼如下
profile_data = {'Authorization': 'Bearer ' + access_token}

profile = requests.post('https://api.line.me/v2/profile/', headers=profile_data)

user_id = str(profile['userId'])
displayName = str(profile['displayName'])
pictureUrl = str(profile['pictureUrl'])
statusMessage = str(profile['statusMessage'])

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
dragonH
iT邦超人 5 級 ‧ 2019-07-28 14:18:37

根據 line api doc

401 就是認證沒過

沒啥好說的

所以你可以檢查你的 token 是否正確無誤

再檢查你的 header 是否格式正確

Authorization: `Bearer ${token}`

附上我測試的 code

不過我是用 nodejs

const Koa = require('koa');
const Router = require('koa-router');
const BodyParser = require('koa-bodyparser');
const axios = require('axios');

const app = new Koa();
const router = new Router();
const port = 9999;
const profileUrl = 'https://api.line.me/v2/bot/profile/';
const token = 'C..FU=';

router.post('/', (ctx) => {
  const { userId } = ctx.request.body.events[0].source;
  ctx.status = 200;
  const headers = {
    Authorization: `Bearer ${token}`,
  };
  axios.default.get(`${profileUrl}${userId}`, { headers })
    .then(res => console.log(res.data))
    .catch(err => console.log(err));
})
app.use(BodyParser());
app.use(router.routes());
app.listen(port, () => {
  console.log(`Server is running at http://127.0.0.1:${port}`);
});

ps.

補充一下

這 api 應該是要用 get

而不是 post

不過這不是造成 401 的 原因就是
補上 python 測試

import requests
access_token = 'C..='
userId = 'U..d'
profile_data = {'Authorization': 'Bearer ' + access_token}

profile = requests.get('https://api.line.me/v2/bot/profile/'+ userId, headers=profile_data)
print(profile.text)
看更多先前的回應...收起先前的回應...

感謝dragonH大大

請問d大,怎麼在登入後先取得user_id呢?

補充一下:
我是透過line login登入後
https://notify-bot.line.me/oauth/authorize?response_type=code&client_id=xxxxxx&redirect_uri=https://xxxxxxxx.herokuapp.com&scope=notify&state=abcde
會回傳
https://xxxxxxxx.herokuapp.com?code=xxxxxxx&state=abcde
利用code
透過POST https://notify-bot.line.me/oauth/token
取回access_token

我現在卡在
怎麼用取回的這個access_token
取得user_profile呢?

謝謝dragonH大大

dragonH iT邦超人 5 級 ‧ 2019-07-28 20:03:52 檢舉

所以你是用哪個 api

line notify 嗎

dragonH iT邦超人 5 級 ‧ 2019-07-28 20:55:29 檢舉

然後你的作法好像有點怪怪的

透過

https://notify-bot.line.me/oauth/token

所拿到的token 是讓你用在 notify api 的

而 message api 的

https://api.line.me/v2/bot/profile/

所需要的 token

是 channel access token

然後你想要登入後可以取得 user profile

則應該是用 login api

/images/emoticon/emoticon16.gif

感謝d大
我是用notify api
去回傳訊息
請問您最後說的login api
有辦法跟我現在的作法結合嗎?

dragonH iT邦超人 5 級 ‧ 2019-07-29 09:04:51 檢舉

如果你的需求是 user 在你的網站用 line 登入後

server 可以取得他的 line profile

答案是可以的喔

根據 Api doc

After getting an access token, use it to call the Social API to get user profile information, log out the user, and manage access tokens. For more information, see the following pages.

這裡取得的 token 可以用在 line social api

也就可以取得 user 的 profile

感謝d大
補充一下,我的需求是user登入後
可以取得user profile和notify token

請問有辦法結合嗎?

感謝d大
補充一下,我的需求是user登入後
可以取得user profile和notify token

請問有辦法結合嗎?

dragonH iT邦超人 5 級 ‧ 2019-07-30 13:10:04 檢舉

你要先了解

login api

notify api

是完全不相關的

有這個了解後再來看你的功能要如何達成

可能你有個登入頁面

user 透過 login api 登入後

到某個頁面

然後該頁面有個按鈕

能夠將 user 導向 連動 notify 的頁面

感謝d大
我已做了一個連動notify的頁面
請問user登入後
該怎麼將user導向您說的notify的頁面呢?

dragonH iT邦超人 5 級 ‧ 2019-08-04 13:24:24 檢舉

jason879797

你應該是要先實作一個 串接 login api 的登入頁面

這樣 user 透過這個頁面登入後

你就能獲取他的 userid 之類的東西

進而取得你需要的 user profile


notify api

會需要將 user 導向授權頁面來作連動

你可以用個按鈕或連結來將 user 導向那個頁面

dragonH
感謝d大
經過漫長爬文
最後用
render_template('index.html')
完成囉!!
謝謝d大的教導(敬禮)

我要發表回答

立即登入回答