Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Created January 9, 2012 03:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deadprogram/1580859 to your computer and use it in GitHub Desktop.
Save deadprogram/1580859 to your computer and use it in GitHub Desktop.
Ruby Sample Application for AT&T Alpha API Foundry

Connecting To An Alpha API Foundry Service - The Basics

Getting Ready

  • Determine Where Your App Is Deployed
  • Register your application with AT&T Developer portal
  • Obtain a client_id and client_secret for your app from AT&T Developer portal

Making The Call

  • Request authorization code on the User's behalf from AT&T OAuth server
  • AT&T OAuth Server redirects back to your app with OAuth code
  • Use returned oauth code to request OAuth access token
  • Use OAuth access token to call API

Authorization

Your app will need to be given permission by each user in order to access their personal data. Here is how it works:

  • The app will navigate to the “Consent Request” page automatically, whenever the app needs to obtain authorization to access user data
  • User enters their wireless number, then click on "Allow" button
  • An SMS will be sent to the user’s phone, with a PIN to allow the app access
  • User enters the PIN from their phone into their web browser, then clicks on “OK” button
  • User should see “Thank you” message then must click on the "Close Window" button (NOT "Sign up Now") to complete the authorization process
  • Now the application is authorized to call APIs on the user's behalf

Location API

The Location API allows you to return the current location for a specific mobile device, based on the phone number. The user will need to give permission to the application that wants to track the mobile device, and the application will only be able to get the location data for the user’s own phone.

You need to pass in both the telephone number you want to obtain location data for, along with the requestedAccuracy parameter, which determines the precision of how the calculation to determine the phone's location is performed.

How To Run The Sample Code

bundle install
FOUNDRY_CLIENT_ID=<your client ID here> FOUNDRY_CLIENT_SECRET=<your secret here> ruby helloapp.rb
require File.expand_path("helloapp", File.dirname(__FILE__))
run Sinatra::Application
source :rubygems
gem 'rack', "~> 1.3.6"
gem 'sinatra'
gem 'json'
gem 'oauth2'
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'json'
enable :sessions
def client
OAuth2::Client.new((ENV['FOUNDRY_CLIENT_ID']||'testing'),
(ENV['FOUNDRY_CLIENT_SECRET']||'testing'),
:site => 'http://apifoundry-dev.pao1.tfoundry.com',
:authorize_url => 'http://apifoundry-dev.pao1.tfoundry.com/oauth/authorize',
:token_url => 'http://apifoundry-dev.pao1.tfoundry.com/oauth/access_token')
end
get "/" do
erb :index
end
get '/auth' do
session[:phone] = params[:phone].delete "-"
authorization_url = client.auth_code.authorize_url(:redirect_uri => redirect_uri, :response_type => "client_credentials", :scope => "TL")
puts "Redirecting to URL: #{authorization_url.inspect}"
redirect authorization_url
end
get '/auth/callback' do
begin
access_token = client.auth_code.get_token(params[:code], :redirect_uri => redirect_uri)
api_url = "/1/devices/tel:#{session[:phone]}/location?access_token=#{access_token.token}&requestedAccuracy=1000"
location = JSON.parse(access_token.get(api_url).body)
erb "<p>Your location:\n#{location.inspect}</p>"
rescue OAuth2::Error => e
erb %(<p>#{$!}</p><p><a href="/auth">Retry</a></p>)
end
end
get '/auth/failure' do
erb "<h1>Authentication Failed:</h1><h3>message:<h3> <pre>#{params}</pre>"
end
def redirect_uri(path = '/auth/callback', query = nil)
uri = URI.parse(request.url)
uri.path = path
uri.query = query
uri.to_s
end
__END__
@@ index
<form action="/auth">
<label for="phone">Phone Number</label>
<input type="text" name="phone" id="phone" />
<input type="submit" value="Locate" />
<form>
@@ layout
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AT&T Alpha API Foundry - Example App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Le styles -->
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">AT&T Alpha API Foundry - Example App</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="/">Home</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<%= yield %>
</div> <!-- /container -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment