Created
November 16, 2011 01:05
-
-
Save rbartholomew/1368949 to your computer and use it in GitHub Desktop.
Netflix Windows 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function login() { | |
var cllb = Windows.Security.Authentication.Web.WebAuthenticationBroker.getCurrentApplicationCallbackUri(); | |
//then use the web broker to all user to sign in | |
loginUrl += "&application_name=" + applicationName; | |
loginUrl += "&oauth_callback=" + cllb.absoluteUri; | |
loginUrl += "&oauth_consumer_key=" + clientKey; | |
var url = new Windows.Foundation.Uri(loginUrl); | |
return Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync(Windows.Security.Authentication.Web.WebAuthenticationOptions.default, url, cllb); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setData(recObject) { | |
var source = new WinJS.UI.ArrayDataSource(recObject.recommendations.recommendation); | |
var basicListView = WinJS.UI.getControl(document.getElementById("recListView")); | |
basicListView.dataSource = source; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>NetflixSample</title> | |
<!-- WinJS references --> | |
<link rel="stylesheet" href="/winjs/css/ui-dark.css" /> | |
<script src="/winjs/js/base.js"></script> | |
<script src="/winjs/js/wwaapp.js"></script> | |
<script src="/winjs/js/ui.js"></script> | |
<script src="/winjs/js/binding.js"></script> | |
<script src="/winjs/js/controls.js"></script> | |
<script src="/winjs/js/res.js"></script> | |
<script src="/winjs/js/animations.js"></script> | |
<script src="/winjs/js/uicollections.js"></script> | |
<!-- NetflixSample references --> | |
<link rel="stylesheet" href="/css/default.css" /> | |
<script src="/js/default.js"></script> | |
<script src="/js/Netflix.js"></script> | |
</head> | |
<body> | |
<div id="itemTemplate" data-win-control="WinJS.Binding.Template"> | |
<div> | |
<img class="itemPicture" data-win-bind="src : box_art.medium" /> | |
<div class="itemTitle" data-win-bind="innerText: title.regular"></div> | |
</div> | |
</div> | |
<div id="recListView" data-win-control="WinJS.UI.ListView" data-win-options="{itemRenderer: itemTemplate}"> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
// Uncomment the following line to enable first chance exceptions. | |
// Debug.enableFirstChanceException(true); | |
WinJS.Application.onmainwindowactivated = function (e) { | |
if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) { | |
Netflix.requestToken().then(function(){ | |
return Netflix.getRecommendations(); | |
}).then(function(data){ | |
var rec = JSON.parse(data.response); | |
setData(rec); | |
}); | |
} | |
} | |
document.addEventListener("DOMContentLoaded", function (e) { | |
WinJS.UI.processAll(); | |
}); | |
function setData(recObject) { | |
var source = new WinJS.UI.ArrayDataSource(recObject.recommendations.recommendation); | |
var basicListView = WinJS.UI.getControl(document.getElementById("recListView")); | |
basicListView.dataSource = source; | |
} | |
WinJS.Application.start(); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
function requestToken() { } | |
function getRecommendations() { } | |
WinJS.Namespace.define('Netflix', | |
{ | |
requestToken: requestToken, | |
getRecommendations: getRecommendations, | |
}); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
var clientKey = "XXXXXXXXXXXXXX"; | |
var clientSecret = "XXXXXXXXXXXX"; | |
var oauthToken; | |
var oauthSecret; | |
var userId; | |
var loginUrl; | |
var applicationName; | |
var requestUri = "http://api.netflix.com/oauth/request_token"; | |
var requestTokenUrl = "http://api.netflix.com/oauth/request_token"; | |
function sendRequest(url){ | |
return WinJS.xhr({ type: "GET", url: url }); | |
} | |
function saveData() { | |
var accessKeys = {}; | |
accessKeys.oauthToken = oauthToken; | |
accessKeys.oauthSecret = oauthSecret; | |
accessKeys.userId = userId; | |
window.localStorage["Netflix"] = JSON.stringify(accessKeys); | |
} | |
function loadData() { | |
var accessKeys = window.localStorage["Netflix"]; | |
if (accessKeys != "" && accessKeys != undefined) { | |
accessKeys = JSON.parse(accessKeys); | |
oauthToken = accessKeys.oauthToken; | |
oauthSecret = accessKeys.oauthSecret; | |
userId = accessKeys.userId; | |
return true; | |
} | |
return false; | |
} | |
function sendPostRequest(url, postData){ | |
return WinJS.xhr({ type: "POST", url: url, headers: { Authorization: postData } }); | |
} | |
function getRecommendations(){ | |
var url = "http://api.netflix.com/users/" + userId + "/recommendations"; | |
var data = createSignedGetRequest(url); | |
return sendRequest(url + data); | |
} | |
function requestToken() { | |
if (loadData()) { | |
return new WinJS.Promise( | |
function (complete) { | |
complete(); | |
}); | |
} | |
var timestamp = Math.round(new Date().getTime() / 1000.0); | |
var nonce = Math.random(); | |
nonce = Math.floor(nonce * 1000000000); | |
//Creating the signature, the parameters need to be defined in alphabetical order. | |
var sigBaseStringParams = "oauth_consumer_key=" + clientKey; | |
sigBaseStringParams += "&" + "oauth_nonce=" + nonce; | |
sigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1"; | |
sigBaseStringParams += "&" + "oauth_timestamp=" + timestamp; | |
sigBaseStringParams += "&" + "oauth_version=1.0"; | |
var sigBaseString = "POST&"; | |
sigBaseString += encodeURIComponent(requestTokenUrl) + "&" + encodeURIComponent(sigBaseStringParams); | |
var keyText = clientSecret + "&"; | |
var keyMaterial = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(keyText, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); | |
var macAlgorithmProvider = Windows.Security.Cryptography.Core.MacAlgorithmProvider.openAlgorithm("HMAC_SHA1"); | |
var key = macAlgorithmProvider.createKey(keyMaterial); | |
var tbs = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(sigBaseString, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); | |
var signatureBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.sign(key, tbs); | |
var signature = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(signatureBuffer); | |
var dataToPost = "OAuth oauth_consumer_key=\"" + clientKey + "\", oauth_nonce=\"" + nonce + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + timestamp + "\", oauth_version=\"1.0\", oauth_signature=\"" + encodeURIComponent(signature) + "\""; | |
return sendPostRequest(requestTokenUrl, dataToPost).then(function(response) { | |
return parseRequestAccessResponse(response); | |
}).then(function () { | |
return login(); | |
}).then(function (r) { | |
return loginSuccess(r.responseData); | |
}).then(function() { | |
return completeLogin(); | |
}); | |
} | |
function parseRequestAccessResponse(response) { | |
return new WinJS.Promise( | |
//The initialization function of a Promise can take two more parameters - error and progress. | |
var keyValPairs = response.response.split("&"); | |
function (complete) { | |
for (var i = 0; i < keyValPairs.length; i++) { | |
var splits = keyValPairs[i].split("="); | |
switch (splits[0]) { | |
case "oauth_token": | |
oauthToken = splits[1]; | |
break; | |
case "oauth_token_secret": | |
oauthSecret = splits[1]; | |
break; | |
case "login_url": | |
loginUrl = decodeURIComponent(splits[1]); | |
break; | |
case "application_name": | |
applicationName = splits[1]; | |
break; | |
} | |
} | |
complete(); | |
}); | |
} | |
function login() { | |
var cllb = Windows.Security.Authentication.Web.WebAuthenticationBroker.getCurrentApplicationCallbackUri(); | |
//then use the web broker to all user to sign in | |
loginUrl += "&application_name=" + applicationName; | |
loginUrl += "&oauth_callback=" + cllb.absoluteUri; | |
loginUrl += "&oauth_consumer_key=" + clientKey; | |
var url = new Windows.Foundation.Uri(loginUrl); | |
return Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync(Windows.Security.Authentication.Web.WebAuthenticationOptions.default, url, cllb); | |
} | |
function loginSuccess(response) { | |
return new WinJS.Promise( | |
function (complete) { | |
var data = response; | |
var split = data.split("?"); | |
if (split.length > 1) { | |
var split2 = split[1].split("&"); | |
var tokenPart = split2[0].split("="); | |
oauthToken = tokenPart[1]; | |
} | |
complete(); | |
}); | |
} | |
function completeLogin() { | |
var accessUrl = "http://api.netflix.com/oauth/access_token"; | |
var postData = createSignedPostRequest(accessUrl); | |
return sendPostRequest(accessUrl, postData).then(function(response){ | |
return new WinJS.Promise( | |
function(complete) { | |
var keyValPairs = response.response.split("&"); | |
for (var i = 0; i < keyValPairs.length; i++) { | |
var splits = keyValPairs[i].split("="); | |
switch (splits[0]) { | |
case "oauth_token": | |
oauthToken = splits[1]; | |
break; | |
case "oauth_token_secret": | |
oauthSecret = splits[1]; | |
break; | |
case "user_id": | |
userId = splits[1]; | |
break; | |
} | |
} | |
saveData(); | |
complete(); | |
} | |
); | |
}); | |
} | |
function createSignedGetRequest(url) { | |
var timestamp = Math.round(new Date().getTime() / 1000.0); | |
var nonce = Math.random(); | |
nonce = Math.floor(nonce * 1000000000); | |
var sigBaseStringParams = "oauth_consumer_key=" + clientKey; | |
sigBaseStringParams += "&" + "oauth_nonce=" + nonce; | |
sigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1"; | |
sigBaseStringParams += "&" + "oauth_timestamp=" + timestamp; | |
sigBaseStringParams += "&" + "oauth_token=" + oauthToken; | |
sigBaseStringParams += "&" + "oauth_version=1.0"; | |
sigBaseStringParams += "&" + "output=json"; | |
var sigBaseString = "GET&"; | |
sigBaseString += encodeURIComponent(url) + "&" + encodeURIComponent(sigBaseStringParams); | |
//Once you've authenticated the user, all future requests need to be signed with both you application secret and the | |
//authentication secret. | |
var keyText = clientSecret + "&" + oauthSecret; | |
var keyMaterial = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(keyText, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); | |
var macAlgorithmProvider = Windows.Security.Cryptography.Core.MacAlgorithmProvider.openAlgorithm("HMAC_SHA1"); | |
var key = macAlgorithmProvider.createKey(keyMaterial); | |
var tbs = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(sigBaseString, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); | |
var signatureBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.sign(key, tbs); | |
var signature = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(signatureBuffer); | |
var dataToGet = "?output=json&oauth_consumer_key=" + clientKey + "&oauth_nonce=" + nonce + "&oauth_token=" + oauthToken + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + timestamp + "&oauth_version=1.0&oauth_signature=" + encodeURIComponent(signature); | |
return dataToGet; | |
} | |
function createSignedPostRequest(url) { | |
var timestamp = Math.round(new Date().getTime() / 1000.0); | |
var nonce = Math.random(); | |
nonce = Math.floor(nonce * 1000000000); | |
var sigBaseStringParams = "oauth_consumer_key=" + clientKey; | |
sigBaseStringParams += "&" + "oauth_nonce=" + nonce; | |
sigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1"; | |
sigBaseStringParams += "&" + "oauth_timestamp=" + timestamp; | |
sigBaseStringParams += "&" + "oauth_token=" + oauthToken; | |
sigBaseStringParams += "&" + "oauth_version=1.0"; | |
var sigBaseString = "POST&"; | |
sigBaseString += encodeURIComponent(url) + "&" + encodeURIComponent(sigBaseStringParams); | |
var keyText = clientSecret + "&" + oauthSecret; | |
var keyMaterial = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(keyText, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); | |
var macAlgorithmProvider = Windows.Security.Cryptography.Core.MacAlgorithmProvider.openAlgorithm("HMAC_SHA1"); | |
var key = macAlgorithmProvider.createKey(keyMaterial); | |
var tbs = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(sigBaseString, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); | |
var signatureBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.sign(key, tbs); | |
var signature = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(signatureBuffer); | |
var dataToPost = "OAuth oauth_consumer_key=\"" + clientKey + "\", oauth_nonce=\"" + nonce + "\", oauth_token=\"" + oauthToken + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + timestamp + "\", oauth_version=\"1.0\", oauth_signature=\"" + encodeURIComponent(signature) + "\""; | |
return dataToPost; | |
} | |
WinJS.Namespace.define('Netflix', | |
{ | |
//Any members or functions that need to be available to the rest of the application should be defined here. | |
requestToken: requestToken, | |
getRecommendations: getRecommendations, | |
}); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener("DOMContentLoaded", function (e) { | |
WinJS.UI.processAll(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseRequestAccessResponse(response) { | |
return new WinJS.Promise( | |
function (complete) { | |
var keyValPairs = response.response.split("&"); | |
for (var i = 0; i < keyValPairs.length; i++) { | |
var splits = keyValPairs[i].split("="); | |
switch (splits[0]) { | |
case "oauth_token": | |
oauthToken = splits[1]; | |
break; | |
case "oauth_token_secret": | |
oauthSecret = splits[1]; | |
break; | |
case "login_url": | |
loginUrl = decodeURIComponent(splits[1]); | |
break; | |
case "application_name": | |
applicationName = splits[1]; | |
break; | |
} | |
} | |
complete(); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function saveData() { | |
var accessKeys = {}; | |
accessKeys.oauthToken = oauthToken; | |
accessKeys.oauthSecret = oauthSecret; | |
accessKeys.userId = userId; | |
window.localStorage["Netflix"] = JSON.stringify(accessKeys); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Netflix.requestToken().then(function(){ | |
return Netflix.getRecommendations(); | |
}).then(function(data){ | |
var rec = JSON.parse(data.response); | |
setData(rec); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment