LoginSignup
0
0

More than 5 years have passed since last update.

LinkedInAPIを使う。

Last updated at Posted at 2018-10-29

LinkedInのAPIで何ができるの?

  • 結論。大したことはできない。。。

とりあえずつなげるまで

appの登録

ここからアプリを作るキャプチャ.PNG

  • 適当に設定。とりあえず動かすにはなんでもいいので頑張らない
    キャプチャ.PNG

  • 設定すると「Client ID」と「Client Secret」が払い出されるので、メモっとく

  • 「RedirectURL」を求められるのでちゃんと入れる。アプリからLinkedInの認証画面に飛んでOKとなった後に遷移する画面。とりあえず適当にエントリーポイント作っておく。

キャプチャ.PNG

自分のアカウントでアプリの許可

  • CLIENT_IDとREDIRECT_URIを使って、Appの許可を行う。
https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&state=987654321&scope=r_basicprofile
  • 許可すると、REDIRECT_URIにcodeパラメータが付与されてリダイレクトされる。

トークンを取得

      curl -X POST \
        --data "grant_type=authorization_code" \
        --data "code={code}" \
        --data "redirect_uri={REDIRECT_URI}" \
        --data "client_id={CLIENT_ID}" \
        --data "client_secret={CLIENT_SECRET}" \
        "https://www.linkedin.com/oauth/v2/accessToken"

  • するとトークンが返ってくるので、それを使って各APIをたたく。
  • 本当はprofileAPIとか使いたかったが、パーミッションがなくてたたけない。
  • パーミッションを追加するにはここからいろいろ登録が必要とのこと。審査も必要なので残念。。。

APIをたたく

  • せっかくなので、goで書いておく

    reqURL := "https://api.linkedin.com/v1/people/~?format=json"
    token := "{token}"
    req, err := http.NewRequest(
        "GET",
        reqURL,
        nil,
    )
    if err != nil {
        fmt.Println(err)
    }
    req.Header.Set("Authorization", "Bearer "+token)
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()

    respBody, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(respBody))

  • こんな感じ
  • 本当はつながってるユーザの情報がとりたかったが、いけてない。。。。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0