Receive response from Instagram API call with AngularJS
<p>To authenticate a user in an app using the Instagram API, you redirect the user to the URL <code>https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&amp;redirect_uri=REDIRECT-URI&amp;response_type=code</code>.</p>

<p>Once the user signs in on this page, Instagram then makes a <code>GET</code> request to the redirect URI (<code>REDIRECT-URI</code> in the above URL). This call to the redirect URI contains a code which you must then include in a <code>POST</code> request to <code>https://api.instagram.com/oauth/access_token</code> to exchange for an access token which is sent in a <code>JSON</code> form in the response.</p>

<p>So I'm receiving the redirect call and making the request for the access token with my Express API and this works fine and I get the form with the access token. </p>

<pre><code>app.get('/redirect', function(req, res){
    if(req.query.error){
        console.log('Error authenticating user');
        console.log(req.query.error);
    }
    else{
        request.post(
                     'https://api.instagram.com/oauth/access_token',
                     {form:{
                             client_id: 'my_client_id',
                                 client_secret: 'my_client_secret',
                                 grant_type: 'authorization_code',
                                 redirect_uri: 'http://localhost/redirect',
                                 code: req.query.code
                                 }
                     },
                     function(error, response, body){
                         if(!error &amp;&amp; response.statusCode == 200){
                             console.log(body);
                             //HOW DO I GET THIS RESPONSE TO ANGULAR?
                             res.json({
                                     access_token: body.access_token,
                                         full_name: body.full_name,
                                         username: body.username,
                                         profile_picture: body.profile_picture,
                                         id: body.id
                                         });
                             return;
                         }
                         else{
                             console.log(response);
                             console.log(body);
                         }
                     }

                     );
    };
</code></pre>

<p><b>My question is:</b> how to I send the access token (or error response) back to Angular?
In other words, how does Angular receive the response from a request it did not make (since the response I want to receive is from the request made with NodeJS's <code>request</code>)?</p>

<p>Initially I tried to make the first <code>GET</code> to <code>https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&amp;redirect_uri=REDIRECT-URI&amp;response_type=code</code> with Angular but I don't think this can be done because the user should be directed to the Instagram URL to sign in.</p>

<hr>

<p><b>Update:</b></p>

<p>When Instagram makes the <code>GET /redirect?code=xxxxxx</code>, it sends the request to my API but also changes the page URL to <code>http://localhost/redirect?code=xxxxxx</code>. I can receive the access token in my API and send it to my client-side but because of the above URL, the page is just displayed as the text of the response and not my <code>index.html</code> with <code>ng-view</code>. How do I get the page to be my <code>index.html</code> in this situation?</p>

<p>I've tried with angular-router the following ways but neither worked:</p>

<pre><code>.when('/redirect', {
        templateUrl: 'views/success.html',
                controller: 'Controller'
                        })
</code></pre>

<p>and</p>

<pre><code>.when('/success', {
        templateUrl: 'views/success.html',
                controller: 'Controller'
                        })
.when('/redirect', {
        redirectTo: '/success'
                        })
</code></pre>