anastasiaalt / finalProject

0 stars 0 forks source link

The Weakest Link(edIn) Is ME #4

Open anastasiaalt opened 8 years ago

anastasiaalt commented 8 years ago

Link and code below. https://github.com/anastasiaalt/finalProject/blob/master/app.js

Getting an error I can see in terminal connected to manner in which I am using request. I embedded the request.get inside the request.post so I could use the accessToken. I originally tried it outside but I didn't see how I could access the data.

SyntaxError: Unexpected token <
    at Object.parse (native)
    at Request._callback (/Users/anastasiaalt/finalProject/app.js:155:26)
    at Request.self.callback (/Users/anastasiaalt/finalProject/node_modules/request/request.js:198:22)

I am also not sure how to connect the data I get from LinkedIn to my users collection given the User model I have does not fit with the information being gathering from LinkedIn. I took a shot below but can't even get code running that far to see what kind of an error I would get there.

app.get('/auth/linkedin/callback', function(req, res){
  // I know the query parameters in the URL but don't know how to render them in the path
  console.log(req.query.code);
  console.log('Above is Access Code');
  var params = {
    grant_type: 'authorization_code',
    code: req.query.code,
    redirect_uri: callbackUrl,
    client_id: clientID,
    client_secret: clientSecret
  };
  var url = 'https://www.linkedin.com/uas/oauth2/accessToken';
  // request is allowing you to make a post from within this get
  request.post(url, {form: params}, function(err, res, body){
    var accessToken = JSON.parse(body).access_token;
    // What should come back at the response is the access token
    req.session.accessToken = accessToken;
    console.log(req.session.accessToken);
    console.log('Above is Access Token');

    console.log(req.session);
    console.log('Above is Req.Session');
    console.log(session);
    console.log('Above is Session');
    // store access token in session
    // use access token to make request to LinkedIn for basic profile information
    // res.redirect('/');

    var options = {
      connection: 'Keep-Alive',
      authorization: 'Bearer'+accessToken,
    };
      console.log('Bearer'+accessToken);
      console.log(options);
    var finalURL = 'https://api.linkedin.com/v1/people/~';
    request.get(finalURL, {form: options}, function(err, res, body){
      var profile = JSON.parse(body).authorization;
      console.log(profile);
      var user = new User;
      var user = profile;
      console.log(user);
      user.save(function(err, result) {
        console.log(req.body);
      res.redirect('/');
      });
    });
  });
});
phlco commented 8 years ago

you have an unexpected token when you parse the data. are you sure it's coming back as json? you might try console.logging the response. as for the headers, in the example code "authorization" is capitalized. does that matter? also, those are headers and shouldnt be included as form data. how do you configure headers with the request module? finally if your schema data doesnt conform you can always change it or just keep user in session.