Skip to content

Instantly share code, notes, and snippets.

@t2-support-gists
Created May 3, 2012 20:59
Show Gist options
  • Select an option

  • Save t2-support-gists/2589439 to your computer and use it in GitHub Desktop.

Select an option

Save t2-support-gists/2589439 to your computer and use it in GitHub Desktop.
DC Java app1
AT&T API Samples - DC app 1
------------------------------
This file describes how to set up, configure and run the Java Applications of the AT&T HTML5 Program sample applications.
It covers all steps required to register the application on DevConnect and, based on the generated API keys and secrets,
create and run one's own full-fledged sample applications.
1. Configuration
2. Installation
3. Parameters
4. Running the application
1. Configuration
Configuration consists of a few steps necessary to get an application registered on DevConnect with the proper services and
endpoints, depending on the type of client-side application (autonomous/non-autonomous).
To register an application, go to https://devconnect-api.att.com/ and login with your valid username and password.
Next, choose "My Apps" from the bar at the top of the page and click the "Setup a New Application" button.
Fill in the form, in particular all fields marked as "required".
Be careful while filling in the "OAuth Redirect URL" field. It should contain the URL that the oAuth provider will redirect
users to when he/she successfully authenticates and authorizes your application. For this application, it should point to
the oauth.jsp file inside this application folder. For example, if running on a local machine in a default Tomcat installation,
your OAuth Redirect URL might be http://localhost:8080/SampleApp/oauth.jsp
NOTE: You MUST select Device Capabilities in the list of services under field 'Services' in order to use this sample application code.
Having your application registered, you will get back an important pair of data: an API key and Secret key. They are
necessary to get your applications working with the AT&T HTML5 APIs. See 'Adjusting parameters' below to learn how to use
these keys.
Initially your newly registered application is restricted to the "Sandbox" environment only. To move it to production,
you may promote it by clicking the "Promote to production" button. Notice that you will get a different API key and secret,
so these values in your application should be adjusted accordingly.
Depending on the kind of authentication used, an application may be based on either the Autonomous Client or the Web-Server
Client OAuth flow (see https://devconnect-api.att.com/docs/oauth20/autonomous-client-application-oauth-flow or
https://devconnect-api.att.com/docs/oauth20/web-server-client-application-oauth-flow respectively).
2. Installation
** Requirements
To run the examples you need a Java environment and at least Apache Tomcat 6, or another Java web server such as Jetty.
** Setting up multiple sample applications simultaneously
In case multiple applications need to be run at the same time, make sure to put each app in a separate folder and
adjust your OAuth Redirect URL accordingly.
3. Parameters
Each sample application contains a config.jsp file. It holds configurable parameters described in an easy to read format.
Please populate the following parameters in config.jsp as specified below:
1) clientIdWeb : {set the value as per your registered appliaction 'API key' field value}
2) clientSecretWeb : {set the value as per your registered appliaction 'Secret key' field value}
3) FQDN : https://api.att.com
Note: If your application is promoted from Sandbox environment to Production environment and you decide to use production
application settings, you must update parameters 1-2 as per production application details.
4. Running the application
To run the application, put the entire contents of the application folder into a separate folder named SampleApp inside the webapps
folder in your Apache Tomcat home directory. If you have specified a different home directory in Tomcat for your web applications,
put it there instead.
Depending on your security settings in Apache Tomcat, you might need to enable write access to the OauthStorage.jsp file.
Once you start tomcat, typically using the command "<your-tomcat-root-folder>/bin/startup.sh", your application becomes available
in a web browser, so you may visit: http://localhost:8080/SampleApp/DC.jsp to see it working.
<%
String clientIdWeb = "";
String clientSecretWeb = "";
String FQDN = "https://api.att.com";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
<title>AT&T Sample DC Application - Get Device Capabilities Application</title>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="style/common.css"/ >
<script type="text/javascript" src="js/helper.js">
</script>
<body>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="org.apache.commons.httpclient.*"%>
<%@ page import="org.apache.commons.httpclient.methods.*"%>
<%@ page import="org.json.JSONObject"%>
<%@ page import="java.util.Iterator"%>
<%@ include file="config.jsp" %>
<%
String scope = (String) session.getAttribute("scope");
if(scope==null) scope="";
String postOauth = "DC.jsp?getDeviceInfo=true";
String redirectUri = "";
String address = request.getParameter("address");
if(address==null || address.equalsIgnoreCase("null"))
address = (String) session.getAttribute("addressDC");
if(address==null || address.equalsIgnoreCase("null"))
address = "425-802-8620";
Boolean newAddress = false;
if(!address.equalsIgnoreCase((String)session.getAttribute("addressDC"))) {
newAddress = true;
}
session.setAttribute("addressDC",address);
String getDeviceInfo = request.getParameter("getDeviceInfo");
%>
<div id="container">
<!-- open HEADER --><div id="header">
<div>
<div id="hcRight">
<%=new java.util.Date()%>
</div>
<div id="hcLeft">Server Time:</div>
</div>
<div>
<div id="hcRight"><script language="JavaScript" type="text/javascript">
var myDate = new Date();
document.write(myDate);
</script></div>
<div id="hcLeft">Client Time:</div>
</div>
<div>
<div id="hcRight"><script language="JavaScript" type="text/javascript">
document.write("" + navigator.userAgent);
</script></div>
<div id="hcLeft">User Agent:</div>
</div>
<br clear="all" />
</div><!-- close HEADER -->
<div id="wrapper">
<div id="content">
<h1>AT&T Sample DC Application - Get Device Capabilities Application</h1>
<h2>Feature 1: Get Device Capabilities</h2>
</div>
</div>
<form method="post" name="getDeviceInfo" action="DC.jsp">
<div id="navigation">
<table border="0" width="100%">
<tbody>
<tr>
<td width="20%" valign="top" class="label">Phone:</td>
<td class="cell"><input maxlength="16" size="12" name="address" value="<%=address%>" style="width: 90%">
</td>
</tr>
</tbody></table>
</div>
<div id="extra">
<table border="0" width="100%">
<tbody>
<tr>
<td class="cell"><button type="submit" name="getDeviceInfo">Get Device Capabilities</button>
</td>
</tr>
</tbody></table>
</div>
<br clear="all" />
</form>
<%
if(getDeviceInfo!=null) {
//Check for a few known formats the user could have entered the address, adjust accordingly
String invalidAddress = null;
if((address.indexOf("-")==3) && (address.length()==12))
address = "tel:" + address.substring(0,3) + address.substring(4,7) + address.substring(8,12);
else if((address.indexOf(":")==3) && (address.length()==14))
address = address;
else if((address.indexOf("-")==-1) && (address.length()==10))
address = "tel:" + address;
else if((address.indexOf("-")==-1) && (address.length()==11))
address = "tel:" + address.substring(1);
else if((address.indexOf("-")==-1) && (address.indexOf("+")==0) && (address.length()==12))
address = "tel:" + address.substring(2);
else
invalidAddress = "yes";
if(invalidAddress==null) {
String accessToken = request.getParameter("access_token");
if(accessToken==null || accessToken=="null"){
accessToken = (String) session.getAttribute("accessToken");}
if((newAddress==true) || (accessToken==null) || (!scope.equalsIgnoreCase("DC")) && (!scope.equalsIgnoreCase("SMS,MMS,WAP,DC,TL,PAYMENT"))) {
session.setAttribute("scope", "DC");
session.setAttribute("clientId", clientIdWeb);
session.setAttribute("clientSecret", clientSecretWeb);
session.setAttribute("address", address);
session.setAttribute("postOauth", postOauth);
session.setAttribute("redirectUri", redirectUri);
response.sendRedirect("oauth.jsp?getExtCode=yes");
}
String url = FQDN + "/1/devices/" + address + "/info";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
method.setQueryString("access_token=" + accessToken);
method.addRequestHeader("Accept","application/json");
int statusCode = client.executeMethod(method);
if(statusCode==200) {
JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
JSONObject deviceId = new JSONObject(jsonResponse.getString("deviceId"));
JSONObject capabilities = new JSONObject(jsonResponse.getString("capabilities"));
Iterator deviceIdKeys = deviceId.keys();
Iterator capabilitiesKeys = capabilities.keys();
%>
<div class="successWide">
<strong>SUCCESS:</strong><br />
Device parameters listed below.
</div>
<br />
<div align="center">
<table width="500" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
<th width="50%" class="label">Parameter</th>
<th width="50%" class="label">Value</th>
</tr>
</thead>
<tbody>
<%
while(deviceIdKeys.hasNext()) {
String key = (String) deviceIdKeys.next();
%>
<tr>
<td class="cell" align="center"><em><%=key%></em></td>
<td class="cell" align="center"><em><%=deviceId.getString(key)%></em></td>
</tr>
<%
}
%>
<%
while(capabilitiesKeys.hasNext()) {
String key = (String) capabilitiesKeys.next();
%>
<tr>
<td class="cell" align="center"><em><%=key%></em></td>
<td class="cell" align="center"><em><%=capabilities.getString(key)%></em></td>
</tr>
<%
}
%>
</tbody>
</table>
</div>
<br />
<%
}
else {
%>
<div class="errorWide">
<strong>ERROR:</strong><br />
<%=method.getResponseBodyAsString()%>
</div>
<br />
<%
}
method.releaseConnection();
} else { %>
<div class="errorWide">
<strong>ERROR:</strong><br />
Invalid Address Entered
</div><br/>
<% }
}
%>
<div id="footer">
<div style="float: right; width: 20%; font-size: 9px; text-align: right">Powered by AT&amp;T Virtual Mobile</div>
<p>&#169; 2011 AT&amp;T Intellectual Property. All rights reserved. <a href="http://developer.att.com/" target="_blank">http://developer.att.com</a>
<br>
The Application hosted on this site are working examples intended to be used for reference in creating products to consume AT&amp;T Services and not meant to be used as part of your product. The data in these pages is for test purposes only and intended only for use as a reference in how the services perform.
<br>
For download of tools and documentation, please go to <a href="https://devconnect-api.att.com/" target="_blank">https://devconnect-api.att.com</a>
<br>
For more information contact <a href="mailto:developer.support@att.com">developer.support@att.com</a>
</div>
</div>
</body></html>
<!--
Licensed by AT&T under 'Software Development Kit Tools Agreement.' September 2011
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: http://developer.att.com/sdk_agreement/
Copyright 2011 AT&T Intellectual Property. All rights reserved. http://developer.att.com
For more information contact developer.support@att.com
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML lang=en xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Application 1</TITLE>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
<SCRIPT type=text/javascript src="helper.js">
</SCRIPT>
<META name=GENERATOR content="MSHTML 8.00.6001.19046"></HEAD>
<BODY>
<TABLE border=0 width="100%">
<TBODY>
<TR>
<TD rowSpan=2 width="25%" align=left><IMG
src="http://developer.att.com/developer/images/att.gif"></TD>
<TD width="15%" align=right>Server Time:</TD>
<TD width="60%" align=left>
<SCRIPT language=JavaScript type=text/javascript>
var myDate = new Date();
document.write(myDate.format('l, F d, Y H:i') + ' PDT');
</SCRIPT>
</TD></TR>
<TR>
<TD width="15%" align=right>Client Time:</TD>
<TD width="25%" align=left>
<SCRIPT language=JavaScript type=text/javascript>
var myDate = new Date();
document.write(myDate.format('l, F d, Y H:i') + ' PDT');
</SCRIPT>
</TD></TR></TBODY></TABLE>
<HR size=px"></HR>
<font size=4px"><B>ATT sample Oauth application</B></font><BR><BR>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="org.apache.commons.httpclient.*"%>
<%@ page import="org.apache.commons.httpclient.methods.*"%>
<%@ page import="org.json.JSONObject"%>
<%@ include file="config.jsp" %>
<%
String clientId = request.getParameter("clientId");
if(clientId!=null)
if(clientId.equalsIgnoreCase("default"))
clientId = clientIdWeb;
if((clientId==null) || (clientId.equalsIgnoreCase("null")))
clientId = (String) session.getAttribute("clientId");
if(clientId==null)
clientId = "";
String clientSecret = request.getParameter("clientSecret");
if(clientSecret!=null)
if(clientSecret.equalsIgnoreCase("default"))
clientSecret = clientSecretWeb;
if((clientSecret==null) || (clientSecret.equalsIgnoreCase("null")))
clientSecret = (String) session.getAttribute("clientSecret");
if(clientSecret==null)
clientSecret = "";
String redirectUri = request.getParameter("redirectUri");
if(redirectUri==null)
redirectUri = (String) session.getAttribute("redirectUri");
if(redirectUri==null)
redirectUri = "https://code-api-att.com/apigee-public/oauth.jsp";
String scope = request.getParameter("scope");
if(scope==null)
scope = (String) session.getAttribute("scope");
if(scope==null)
scope = "SMS,MMS";
session.setAttribute("scope", scope);
String code = request.getParameter("code");
if(code==null) code="";
String print = "";
String getExtCode = request.getParameter("getExtCode");
String refreshToken = request.getParameter("refreshToken");
if (refreshToken==null)
refreshToken=(String) session.getAttribute("refreshToken");
if (refreshToken==null)
refreshToken="";
String getRefreshToken = request.getParameter("getRefreshToken");
if (getRefreshToken==null) getRefreshToken="";
%>
<form name="getExtCode" method="post">
API Key <input type="text" name="clientId" value="default" size=40 /><br>
API Secret <input type="text" name="clientSecret" value="default" size=40 /><br>
{FQDN} <input type="text" name="FQDN" value="<%=FQDN%>" size=60 /><br>
Scope <input type="text" name="scope" value="<%=scope%>" size=40 /><br />
Redirect URI <input type="text" name="redirectUri" value="<%=redirectUri%>" size=60 /><br />
<input type="submit" name="getExtCode" value="Get Access Token" />
</form><br><br>
<%
if(getExtCode!=null) {
session.setAttribute("clientId", clientId);
session.setAttribute("clientSecret", clientSecret);
session.setAttribute("FQDN", FQDN);
response.sendRedirect(FQDN + "/oauth/authorize?client_id=" + clientId + "&scope=" + scope + "&redirect_uri=" + redirectUri);
}
if(!code.equalsIgnoreCase("")) {
String url = FQDN + "/oauth/token";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
String b = "client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=authorization_code&code=" + code;
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
method.setRequestBody(b);
int statusCode = client.executeMethod(method);
print = method.getResponseBodyAsString();
if(statusCode==200){
JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString());
String accessToken = rpcObject.getString("access_token");
refreshToken = rpcObject.getString("refresh_token");
session.setAttribute("refreshToken", refreshToken);
session.setAttribute("accessToken", accessToken);
String postOauth = (String) session.getAttribute("postOauth");
if(postOauth!= null) {
session.setAttribute("postOauth", null);
response.sendRedirect(postOauth);
}
}
method.releaseConnection();
}
%>
<form name="getRefreshToken" method="post">
API Key <input type="text" name="clientId" value="<%=clientId%>" size=40 /><br />
API Secret <input type="text" name="clientSecret" value="<%=clientSecret%>" size=40 /><br />
Refresh Token <input type="text" name="refreshToken" value="<%=refreshToken%>" size=60 /><br />
<input type="submit" name="getRefreshToken" value="Refresh Access Token" />
</form><br><br>
<%
if(!getRefreshToken.equalsIgnoreCase("")) {
session.setAttribute("clientId",clientId);
session.setAttribute("clientSecret",clientSecret);
String url = FQDN + "/oauth/token";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
String b = "client_id=" + clientIdAut + "&client_secret=" + clientSecretAut + "&grant_type=refresh_token&refresh_token=" + refreshToken;
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
method.setRequestBody(b);
int statusCode = client.executeMethod(method);
print = method.getResponseBodyAsString();
if(statusCode==200){
String accessToken = print.substring(18,50);
session.setAttribute("accessToken", accessToken);
String postOauth = (String) session.getAttribute("postOauth");
if(postOauth!= null) {
session.setAttribute("postOauth", null);
response.sendRedirect(postOauth);
} else {
}
}
method.releaseConnection();
}
%>
<br><br><html><body><%=print%></body></html>
<div style="position:absolute;bottom:0px;left:30%;width:40%;font-size:9px;" align="center" >
� 2011 AT&T Intellectual Property. All rights reserved. <a href="http://developer.att.com" target="_blank">http://developer.att.com</a>
<br/>
The Application hosted on this site are working examples intended to be used for reference in creating products to consume AT&T Services and not meant to be used as part of your product. The data in these pages is for test purposes only and intended only for use as a reference in how the services perform.
<br/>
For download of tools and documentation, please go to <a href="https://devconnect-api.att.com" target="_blank">https://devconnect-api.att.com</a>
<br/>
For more information contact <a href="mailto:developer.support@att.com">developer.support@att.com</a>
<br/><br/>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment