RSS

Tag Archives: api

WebApi .Net – Add DelegatingHandler to Get Request Body

Out of the box, WebApi pipeline bind request body to parameter, therefore WebApi Controller (which inherit from ApiController) `Request.Content` object is empty.

For example, passing this json in the request body:

[
    {
        "Name": "Test",
        "Status": "C",
    }
]

Will bind it to WebApi Controller’s parameter, however, `Request.Content` object is empty.

screenshot message handler

Overcome this issue by creating `DelegatingHandler` to add request body back.

RequestHandler.cs

using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace QC
{
    public class RequestHandler : DelegatingHandler
    {
        protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // Request body stream is emptied when WebApi bind request body to parameter, here we add original request body back so we can access it
            if (request.Content != null)
            {
                string body = await request.Content.ReadAsStringAsync();
                request.Properties["body"] = body;
            }

            return await base.SendAsync(request, cancellationToken);
        }
    }
}

Register in WebApiConfig.

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MessageHandlers.Add(new RequestHandler());
    }
}

Viola! `Request.Content` contains request body.

screenshot message handler2

Code is from here.

Advertisements
 
Leave a comment

Posted by on January 18, 2019 in General

 

Tags: , , , , ,

Invoke Google Apps Script with HTTP GET/POST

To be able to invoke Google Apps Script (GAS) through HTTP, the GAS must be deployed as web app, follow guidelines below.

  1. In script editing screen, add `doGet` and `doPost` functions. This following sample functions take a request parameter and simply check for query string parameter `name` if it’s HTTP GET and request body parameter `name` if it’s HTTP POST. Then return JSON object. The functions’s name `doGet` and `doPost` is mandatory Google conventions in order to accept HTTP GET and HTTP POST. For `ContentService` reference see Google docs.
  2. function doGet(request) {
      // Un-comment to see request output
      // return ContentService.createTextOutput(JSON.stringify(request));
    
      // HTTP GET
      var requestContent = request.parameter;
      var responseContent = '{"response": "' + requestContent.name + '"}';
      var response = ContentService.createTextOutput(responseContent);
      // Set return data type
      response.setMimeType(ContentService.MimeType.JSON);
    
      return response;
    }
    
    function doPost(request) {
      // Un-comment to see request output
      // return ContentService.createTextOutput(JSON.stringify(request));
    
      // HTTP POST
      var requestContent = JSON.parse(request.postData.contents);
      var responseContent = '{"response": "' + requestContent.name + '"}';
      var response = ContentService.createTextOutput(responseContent);
      // Set return data type
      response.setMimeType(ContentService.MimeType.JSON);
    
      return response;
    }
    
  3. In script editing screen, click `Publish` menu and `Deploy as web app…`
  4. apps script 1

  5. Configuration window will pop up. Current web app URL is the URL where the GAS can be invoked. Who has access to the app determine authorization setting on who can invoke the URL, the least restricted setting is `Anyone, even anonymous`.
  6. apps script 2

  7. The first time you publish the GAS, Google will ask for authorization.
  8. apps script 5

  9. Google automatically scan the script to identify authorization scopes. Google requires GAS that need sensitive authorization scopes (any scopes that access user data) to be verified by Google. If your GAS is not verified, Google will display warning message (see screen shot below). Simply click `Advanced` and proceed. However, if you plan to distribute your GAS to public it’s good idea to have Google verify your GAS (not discussed in this article).
  10. apps script 6

  11. Invoke GAS like REST API, here is example of HTTP GET invocation of functions we wrote earlier. Notice I pass in parameter `name` in query string which correspond to my code that look for parameter `name`. I use Postman.
  12. apps script 4

  13. And example of HTTP POST.
  14. apps script 3

 
Leave a comment

Posted by on December 26, 2018 in General

 

Tags: , , , , , , , , , , , ,

Facebook Login Integration in ASP.NET MVC

  1. Obtain App Id from Facebook site.
    http://developers.facebook.com/apps
  2. Create new ASP.NET MVC 4 Web Application project (with Internet Application template).

  3. Open the _LoginPartial.cshtml file under Views\Shared folder.
    b
  4. Comment out all code and insert the following:
    <div id="fb-root"></div>
    <script>
        // Load the SDK Asynchronously
        (function(d){
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        }(document));
    
        // Init the SDK upon load
        window.fbAsyncInit = function() {
            FB.init({
                appId      : '<app_id>', // App ID
                channelUrl : '//' + window.location.hostname + '<port>/Home/Channel', // Path to your Channel File
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
            });
    
            // listen for and handle auth.statusChange events
            FB.Event.subscribe('auth.statusChange', function(response) {
                if (response.authResponse) {
                    // user has auth'd your app and is logged into Facebook
                    FB.api('/me', function(me){
                        if (me.name) {
                            document.getElementById('auth-displayname').innerHTML = me.name;
                        }
                    })
                    document.getElementById('auth-loggedout').style.display = 'none';
                    document.getElementById('auth-loggedin').style.display = 'block';
                } else {
                    // user has not auth'd your app, or is not logged into Facebook
                    document.getElementById('auth-loggedout').style.display = 'block';
                    document.getElementById('auth-loggedin').style.display = 'none';
                }
            });
    
            // respond to clicks on the login and logout links
            document.getElementById('auth-loginlink').addEventListener('click', function(){
                FB.login();
            });
            document.getElementById('auth-logoutlink').addEventListener('click', function(){
                FB.logout();
            });
        }
    </script>
    
    <div id="auth-status">
        <div id="auth-loggedout">
            <a href="#" id="auth-loginlink">Login</a>
        </div>
        <div id="auth-loggedin" style="display:none">
            Hi, <span id="auth-displayname"></span>
            (<a href="#" id="auth-logoutlink">logout</a>)
        </div>
    </div>
    
  5. Replace <app_id> in above code with your own app id.
  6. Replace <port> in above code with your application’s port.
    You can get application’s port under Project’s properties window. In the screenshot, it’s 6270.

  7. Create new Action in Home controller, name it Channel.
  8. Add the view for Channel action with no layout or master page (uncheck “Use a layout or master page”).
  9. Edit Channel.cshtml to look as following.
    <script type="text/javascript" src="//connect.facebook.net/en_US/all.js"></script>
    

  10. On Facebook Developer App Settings, set the app to interact with Website. Then set the site’s URL to your application full URL.
  11. Compile and run your ASP.NET MVC application.
  12. Login button on the top right corner should take your users to Facebook login page.
Resource and more readings:
 
7 Comments

Posted by on July 13, 2012 in General

 

Tags: , , , ,

 
%d bloggers like this: