---
# System prepended metadata

title: Imgur Passport-Imgur を使用したOAuth2認証 Node.js　2021-05-10
tags: [Passport-Imgur, Node.js, OAuth2, Imgur]

---

---
lang: ja-jp
breaks: true
---
# Imgur Passport-Imgur を使用したOAuth2認証 Node.js　2021-05-10

> Passport-Imgur
> http://www.passportjs.org/packages/passport-imgur/

> mindfreakthemon/passport-imgur
> https://github.com/mindfreakthemon/passport-imgur

```shell=
$ npm install express
$ npm install passport
$ npm install passport-imgur
```

```javascript=
const passport = require('passport');
const ImgurStrategy = require('passport-imgur').Strategy;
const express       = require("express");

const app = express();
const port = 8080;
const path = require('path');

const IMGUR_CLIENT_ID     = "XXXXXXXXXXX";
const IMGUR_CLIENT_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

passport.use(
    new ImgurStrategy(
        {
            clientID     : IMGUR_CLIENT_ID,
            clientSecret : IMGUR_CLIENT_SECRET,
            //callbackURL  : "http://127.0.0.1:" + port + "/auth/imgur/callback"
            callbackURL  : "https://xxxxxxxxxxx.net/auth/imgur/callback"
        },
    function(accessToken, refreshToken, profile, done) {
        console.log("passport.use");

        console.log("accessToken:" + accessToken);
        console.log("refreshToken:" + refreshToken);
        console.log("profile:" + profile);

// ここで、認証サーバから取得した情報をデータベース等に格納して管理する。
//        User.findOrCreate({ imgurId: profile.id }, function (err, user) {
//            console.log("err:" + err);
//            console.log("user:" + user);
//            return done(err, user);
//        });

        // ↓doneを呼び出すのがミソ。
        return done(accessToken, refreshToken);

        }
    )
);

app.get('/auth/imgur',
    passport.authenticate('imgur'),
    function(req, res) {
        // Successful authentication, redirect home.
        console.log("/auth/imgur body:" + req.body + "");
    }
);

app.get('/auth/imgur/callback', 
    passport.authenticate(
        'imgur', 
        { failureRedirect: '/login' }
        ),
        function(req, res) {
            // Successful authentication, redirect home.
            console.log("/auth/imgur/callback body:" + req.body + " res.redirect");
            res.redirect('/');
            console.log("/auth/imgur/callback body:" + req.body);
        }
    );

app.listen(port, () => { 
        console.log(`Example app listening on port ${port}!`);
    }
);
```

## passport-oauth2-refresh

> fiznool/passport-oauth2-refresh
> https://github.com/fiznool/passport-oauth2-refresh

> ExpressとPassport.jsでOAuth2 (4)有効期限切れのアクセストークンをリフレッシュする
> https://dackdive.hateblo.jp/entry/2017/09/28/100000

###### tags: `Imgur` `Passport-Imgur` `OAuth2` `Node.js`

