Exportera (0) Skriv ut
Visa allt
EN
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,

Send HTTP request

Updated: April 2, 2014

In a JavaScript backend mobile service, server scripts use the node.js request module to make HTTP requests.

The following example validates the specified Twitter username by calling the Twitter REST APIs and only inserts the record when validation succeeds.

function insert(item, user, request) {
    if (!item.twitterScreenName) {
        request.respond(statusCodes.BAD_REQUEST, 
        'You must include a twitter screen name.');
    } else {
        // Get the service configuration module.
        var config = require('mobileservice-config');

        // Get the stored Twitter consumer key and secret. 
        var consumerKey = config.twitterConsumerKey,
            consumerSecret = config.twitterConsumerSecret
        // Get the Twitter access token from app settings.    
        var accessToken= config.appSettings.TWITTER_ACCESS_TOKEN,
            accessTokenSecret = config.appSettings.TWITTER_ACCESS_TOKEN_SECRET;   

        var httpRequest = require('request');
        var url = 'http://api.twitter.com/1.1/users/show.json?screen_name=' + 
        item.twitterScreenName;
        httpRequest.get({
            url: url,
            oauth: {
                consumer_key: consumerKey,
                consumer_secret: consumerSecret,
                token: accessToken,
                token_secret: accessTokenSecret
            }
        }, function(err, response, body) {
            if (err) {
                request.respond(statusCodes.INTERNAL_SERVER_ERROR, 
                'Unable to connect to twitter.');
            } else if (response.statusCode !== 200) {
                request.respond(statusCodes.BAD_REQUEST, 
                'No twitter user found with name ' + 
                item.twitterScreenName + '.');
            } else {
                request.execute();
            }
        });
    }
}

For more information on how to obtain credentials to access Twitter, see the section Get your Twitter OAuth credentials section in the Schedule recurring jobs in Mobile Services tutorial. For more information about scheduling script execution, see the Mobile Services script reference

Visa:
© 2014 Microsoft