好的,所以我正在尝试使用 Javascript 和 WinJS 为 Windows 8 构建一个小型 Twitter 客户端。我已经获得了请求 oauth 令牌以及 oauth 验证程序。Microsoft 的一个模板是一个很好的资源。不过,我似乎无法在网上找到任何关于 Javascript 和 oAuth 的帮助。无论如何,这是我的代码。我需要弄清楚在这里获取 oAuth 访问令牌表格并存储它们。
////
//// The function of this file is to connect with Twitter via oAuth
////
(function () {
"use strict";
//Define a namespace to keep track of controls
WinJS.Namespace.define("Account", {
options: { // This is the object containing the apps
consumerKey: ********, // Twitter credentials
consumerSecret: **********,
callback: *************
}
});
Account.sendRequest = function (url) {
try {
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.send(null);
return request.responseText;
} catch (err) {
WinJS.log("Error sending request: " + err, "Web Authentication SDK Sample", "error");
}
};
Account.sendPostRequest= function(url, authzheader) {
try {
var request = new XMLHttpRequest();
request.open("POST", url, false);
request.setRequestHeader("Authorization", authzheader);
request.send(null);
return request.responseText;
} catch (err) {
WinJS.log("Error sending request: " + err, "Web Authentication SDK Sample", "error");
}
};
Account.getVerifier = function (returned) {
var URLstring = returned; //returned;
var oauth_token;
Account.oauth_verifier = "";
URLstring = URLstring.substring("http://novodevelopment.tk/?".length);
var valuePairs = URLstring.split("&");
for (var i = 0; i < valuePairs.length; i++) {
var splits = valuePairs[i].split("=");
switch (splits[0]) {
case "oauth_token":
oauth_token = splits[1];
break;
case "oauth_verifier":
Account.oauth_verifier = splits[1];
console.log("oAuth Verifier: " + Account.oauth_verifier);
break;
}
}
};
Account.convertTokens = function (url, authzheader) {
try {
var request = new XMLHttpRequest();
request.open("POST", url, false);
request.setRequestHeader("Authorization", authzheader);
request.setRequestHeader("oauth_verifier", Account.oauth_verifier);
reqest.send(null);
return request.responseText;
} catch (err) {
console.log("failure");
}
};
Account.authzInProcess = false;
Account.launchTwitterWebAuth = function () {
var twitterURL = "https://api.twitter.com/oauth/request_token";
// Get all parameters
var clientID = Account.options.consumerKey;
var clientSecret = Account.options.consumerSecret;
var callBackURL = Account.options.callback;
// Get Date
var timestamp = Math.round(new Date().getTime() / 1000.0);
// Create a nonce
var nonce = Math.random();
nonce = Math.floor(nonce * 1000000000);
// Compute base signature string and sign it.
// This is a common operation that is required for all requests even after the token is obtained.
// Parameters need to be sorted in alphabetical order
// Keys and values should be URL Encoded.
// To be fair I found all of this part online
// It basically serves the same purpose as jsOAuth
var sigBaseStringParams = "oauth_callback=" + encodeURIComponent(Account.options.callback);
sigBaseStringParams += "&" + "oauth_consumer_key=" + clientID;
sigBaseStringParams += "&" + "oauth_nonce=" + nonce;
sigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1";
sigBaseStringParams += "&" + "oauth_timestamp=" + timestamp;
sigBaseStringParams += "&" + "oauth_version=1.0";
var sigBaseString = "POST&";
sigBaseString += encodeURIComponent(twitterURL) + "&" + 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_callback=\"" + encodeURIComponent(Account.options.callback) + "\", oauth_consumer_key=\"" + clientID + "\", oauth_nonce=\"" + nonce + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + timestamp + "\", oauth_version=\"1.0\", oauth_signature=\"" + encodeURIComponent(signature) + "\"";
var response = Account.sendPostRequest(twitterURL, dataToPost);
var oauth_token;
var oauth_token_secret;
var keyValPairs = response.split("&");
for (var i = 0; i < keyValPairs.length; i++) {
var splits = keyValPairs[i].split("=");
switch (splits[0]) {
case "oauth_token":
oauth_token = splits[1];
break;
case "oauth_token_secret":
oauth_token_secret = splits[1];
break;
}
}
// Send user to authorization page
twitterURL = "https://api.twitter.com/oauth/authorize?oauth_token=" + oauth_token;
var startURI = new Windows.Foundation.Uri(twitterURL);
var endURI = new Windows.Foundation.Uri(Account.options.callback);
Account.authzInProgress = true;
Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync(
Windows.Security.Authentication.Web.WebAuthenticationOptions.none, startURI, endURI)
.done(function (result) {
console.log("Authenticated URL: " + result.responseData);
var returnData = result.responseData;
Account.getVerifier(returnData);
Account.authzInProgress = false;
}, function (err) {
console.log("Error returned by WebAuth broker: " + err, "Web Authentication SDK Sample", "error");
Account.authzInProgress = false;
});
var authzheader = {
oauth_consumer_key: clientID,
oauth_nonce: nonce,
oauth_signature: signature,
oauth_signature_method: "HMAC-SHA1",
oauth_timestamp: timestamp,
oauth_token: oauth_token,
oauth_version: "1.0"
};
var twitterURL = "https://api.twitter.com/oauth/access_token";
// var converted = Account.convertTokens(twitterURL);
Account.convertTokens(twitterURL);
};
})();
抱歉有点乱