gpt4 book ai didi

javascript - 如何在 Windows 8 JavaScript (Metro) 中将 twitter oAuth 请求 token 转换为访问 token

转载 作者:行者123 更新时间:2023-11-28 09:49:42 25 4
gpt4 key购买 nike

好吧,我正在尝试使用 Javascript 和 WinJS 为 Windows 8 构建一个小型 Twitter 客户端。我已经获得了请求 oauth token 以及 oauth 验证器。 Microsoft 的一个模板是这方面的一个很好的资源。不过,我似乎无法在网上找到任何有关 Javascript 和 oAuth 的帮助。无论如何,这是我的代码。我需要弄清楚如何在此处获取 oAuth 访问 token 表单并存储它们。

////
//// 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);

};



})();

抱歉,有点乱

最佳答案

要存储信息,您可以使用 Windows.Storage.ApplicationData允许您存储本地/漫游设置/文件的类。

我建议您使用漫游设置/文件,因为您的应用程序可以通过不同的设备访问,这意味着如果您将应用程序安装在两台设备(例如台式机和笔记本电脑)上,则可以从这两台设备访问存储的信息设备。该关联是通过 LiveID 完成的。

如果您没有使用 LiveID 登录,或者没有互联网连接(在当前情况下,这对于 Twitter 客户端 J 来说会有点问题),设置/文件将保留在本地。

我希望这会有所帮助。

关于javascript - 如何在 Windows 8 JavaScript (Metro) 中将 twitter oAuth 请求 token 转换为访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com