Permalink
Browse files

Script (wl.js) loading endpoint updates and various improvement.

  • Loading branch information...
Lin Wang
Lin Wang committed Dec 6, 2011
1 parent 05fa4b4 commit ac0d4aaec37faae8352ac7e77e23360151e6c10c
@@ -139,7 +139,7 @@
function getUserPicture() {
var session = WL.getSession();
- var LIveConnectAPIUrl = "https://beta.apis.live.net/v5.0";
+ var LIveConnectAPIUrl = "https://apis.live.net/v5.0";
document.getElementById("meImg").src = LIveConnectAPIUrl + "/me/picture?return_ssl_resources=true&access_token=" + session.access_token;
document.getElementById("meImg").style.visibility = "visible";
@@ -37,7 +37,7 @@ public MainPage()
private void OnSessionChanged(Object sender, LiveConnectSessionChangedEventArgs args)
{
- if (args != null && args.Session != null && args.Session.Status == LiveConnectSessionStatus.Connected)
+ if (args != null && args.Session != null && args.Status == LiveConnectSessionStatus.Connected)
{
this.client = new LiveConnectClient(args.Session);
@@ -77,28 +77,21 @@ private void signedInUser()
private void getUserPicture()
{
var memoryStream = new InMemoryRandomAccessStream();
- client.DownloadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(getUserPicture_Callback);
- client.DownloadAsync("/me/picture?return_ssl_resources=true", memoryStream.OpenWrite(), memoryStream);
+ client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(getUserPicture_Callback);
+ client.GetAsync("/me/picture");
}
- private void getUserPicture_Callback(object sender, LiveOperationCompletedEventArgs e)
+ void getUserPicture_Callback(object sender, LiveOperationCompletedEventArgs e)
{
- client.DownloadCompleted -= getUserPicture_Callback;
+ client.GetCompleted -= getUserPicture_Callback;
- if (e.Error == null)
+ if (e.Error == null)
{
- IRandomAccessStream imageStream = e.UserState as IRandomAccessStream;
+ dynamic result = e.Result;
+ BitmapImage imgSource = new BitmapImage();
+ imgSource.UriSource = new Uri(result.location, UriKind.Absolute);
+ this.userImage.Source = imgSource;
- if (imageStream != null)
- {
- BitmapImage imgSource = new BitmapImage();
- imgSource.SetSource(imageStream);
- this.userImage.Source = imgSource;
- }
- else
- {
- statusTxt.Text = "Could not find user's picture";
- }
}
else
{
@@ -108,6 +101,8 @@ private void getUserPicture_Callback(object sender, LiveOperationCompletedEventA
getUserName();
}
+
+
private void getUserName()
{
client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(getUserName_Callback);
@@ -235,7 +230,7 @@ private void createNoteListEntry(String name, String id, String type)
private void deleteNote()
{
- if (client.Session == null || client.Session.Status != LiveConnectSessionStatus.Connected)
+ if (client.Session == null)
{
statusTxt.Text = "You need to Sign In";
}
@@ -274,7 +269,7 @@ private void deleteNote_Callback(object sender, LiveOperationCompletedEventArgs
}
private void saveNote()
{
- if (client.Session == null || client.Session.Status != LiveConnectSessionStatus.Connected)
+ if (client.Session == null )
{
statusTxt.Text = "You need to Sign In";
}
@@ -399,7 +394,7 @@ private void notesList_SelectionChanged(object sender, SelectionChangedEventArgs
private void downloadNote(String noteId)
{
- if (client == null || client.Session == null || client.Session.Status != LiveConnectSessionStatus.Connected)
+ if (client == null || client.Session == null )
{
statusTxt.Text = "You need to Sign In";
}
@@ -410,20 +405,19 @@ private void downloadNote(String noteId)
statusTxt.Text = "Need to save file.";
}
var stream = new MemoryStream();
-
- client.DownloadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(downloadNote_Callback);
- client.DownloadAsync(noteId + "/content?return_ssl_resources=true", stream, stream);
+ client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(downloadNote_Callback);
+ client.DownloadAsync(noteId + "/content?return_ssl_resources=true");
}
}
- private void downloadNote_Callback(object sender, LiveOperationCompletedEventArgs e)
+ private void downloadNote_Callback(object sender, LiveDownloadCompletedEventArgs e)
{
client.DownloadCompleted -= downloadNote_Callback;
if (e.Error == null)
{
// Get the stream with the downloaded file
- var memoryStream = e.UserState as MemoryStream;
+ var memoryStream = e.Result as MemoryStream;
// Cursor is at the end of the stream so we need to rewind
memoryStream.Seek(0, SeekOrigin.Begin);
@@ -459,6 +453,7 @@ private void downloadNote_Callback(object sender, LiveOperationCompletedEventArg
}
}
+
private void backBtn_Click(object sender, RoutedEventArgs e)
{
Application.Current.Exit();
@@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
- <script src="https://beta-js.live.net/v5.0/wl.js" type="text/javascript"></script>
+ <script src="//js.live.net/v5.0/wl.js" type="text/javascript"></script>
</head>
@@ -1,15 +1,15 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
- <script src="https://beta-js.live.net/v5.0/wl.js" type="text/javascript"></script>
+ <script src="//js.live.net/v5.0/wl.js" type="text/javascript"></script>
<script src="js/jquery-1.6.2.min.js" type="text/javascript"></script>
<!-- Shoppinglist references -->
<link rel="stylesheet" href="/css/default.css" />
<script type="text/javascript">
- LIveConnectAPIUrl = "https://beta.apis.live.net/v5.0";
+ LiveConnectAPIUrl = "https://apis.live.net/v5.0";
WL.Event.subscribe("auth.login", onLoginComplete);
WL.Event.subscribe("wl.log", onAPIError);
@@ -74,7 +74,7 @@
function getUserPicture() {
var session = WL.getSession();
- document.getElementById("meImg").src = LIveConnectAPIUrl + "/me/picture?return_ssl_resources=true&access_token=" + session.access_token;
+ document.getElementById("meImg").src = LiveConnectAPIUrl + "/me/picture?return_ssl_resources=true&access_token=" + session.access_token;
document.getElementById("meImg").style.visibility = "visible";
}
@@ -261,20 +261,18 @@ internal void Download()
{
if (SelectedPhoto == null)
return;
- MemoryStream fileContent = new MemoryStream();
-
-
+
+
LiveConnectClient downloadClient = new LiveConnectClient(App.Session);
- downloadClient.DownloadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(downloadClient_DownloadCompleted);
-
- downloadClient.DownloadAsync(SelectedPhoto.ID,fileContent,fileContent);
+ downloadClient.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(downloadClient_DownloadCompleted);
+ downloadClient.DownloadAsync(SelectedPhoto.ID);
}
- void downloadClient_DownloadCompleted(object sender, LiveOperationCompletedEventArgs e)
+ void downloadClient_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
{
if (e.Error == null)
{
- MemoryStream outputStream = (MemoryStream)e.UserState;
+ MemoryStream outputStream = (MemoryStream)e.Result;
// Create a filename for JPEG file in isolated storage.
@@ -290,9 +288,11 @@ void downloadClient_DownloadCompleted(object sender, LiveOperationCompletedEvent
myFileStream.Write(outputStream.GetBuffer(), 0, (int)outputStream.Length);
myFileStream.Close();
}
-
+
}
+
+
internal void Upload()
{
PhotoChooserTask task = new PhotoChooserTask();
@@ -0,0 +1,60 @@
+package com.ms.wlm;
+
+// This file modified from Bare Bones Browser Launch Version 1.5, by Dem Pilafian.
+
+/////////////////////////////////////////////////////////
+//Bare Bones Browser Launch //
+//Version 1.5 (December 10, 2005) //
+//By Dem Pilafian //
+//Supports: Mac OS X, GNU/Linux, Unix, Windows XP //
+//Example Usage: //
+//String url = "http://www.centerkey.com/"; //
+//BareBonesBrowserLaunch.openURL(url); //
+//Public Domain Software -- Free to Use as You Like //
+/////////////////////////////////////////////////////////
+
+/**
+ * @author Dem Pilafian
+ * @author John Kristian
+ */
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import javax.swing.JOptionPane;
+
+public class BareBonesBrowserLaunch {
+
+ public static void openURL(String url) {
+ try {
+ browse(url);
+ } catch (Exception e) {
+ JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + e.getLocalizedMessage());
+ }
+ }
+
+ public static void browse(String url) throws ClassNotFoundException, IllegalAccessException,
+ IllegalArgumentException, InterruptedException, InvocationTargetException, IOException,
+ NoSuchMethodException {
+ String osName = System.getProperty("os.name", "");
+ if (osName.startsWith("Mac OS")) {
+ Class fileMgr = Class.forName("com.apple.eio.FileManager");
+ Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
+ openURL.invoke(null, new Object[] { url });
+ } else if (osName.startsWith("Windows")) {
+ Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
+ } else { // assume Unix or Linux
+ String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
+ String browser = null;
+ for (int count = 0; count < browsers.length && browser == null; count++)
+ if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
+ browser = browsers[count];
+ if (browser == null)
+ throw new NoSuchMethodException("Could not find web browser");
+ else
+ Runtime.getRuntime().exec(new String[] { browser, url });
+ }
+ }
+
+
+
+}
@@ -1,22 +1,64 @@
package com.ms.wlm;
+import java.net.URL;
import java.net.URLDecoder;
+import javax.swing.JOptionPane;
+
public class Program {
- /**
- * The OAuth2.0 access token to be used for login.
- */
- public static final String TestAccessToken = URLDecoder
- .decode("EwAgAq1DBAAUlbRWyAJjK5w968Ru3Cyt%2f6GvwXwAAaaSn%2fzCErnNuCDLPq%2fVeja6u6qXVypLqz8BOpomUMZBS6vKwVrtm5KwTV2wQBldJMahjfFl85tFFAH0853A30FEFl9cmYuV4IdfcAIz1AwiwyRnEfV46KBMbWcKRpokihwvGexejvSIIXyg4d%2fWC6TZmTS4dx9zRxdaUz0DVUBVcatylbMbY3sYpIr7yo8RtFlhcnyCrJzDg89FpeRmTKHEsiouDqFUUenFpR2tiHMJi60UX8QT%2fRJXgC7va7lQJCsbFd1EKPpikjWWlB407ToVxHAep7iDpI%2fIigkJ8nfwS9DgSdmppeVADLAHjOKSnBmhz9TX37%2f0DMA0N%2bNJEEYDZgAACBW6gnbfZbi88AASSaNTyBPyLi7rK13njeIvHx8Lwyb5UyY3WSxtPsl%2f10KV%2fa0q8USJdiP2kx3aP0JP0HJfH%2f7ypQSgvY%2brGRBgcq0IQ9RHMxrKJHb%2bvKBkOPE%2bNtxxY35IdCf6iwaragCUuhYP%2fTt2gKalhpqWmivD5HvrQy%2fFNNu9MszMeDRiOu8vB95kCJeu5RM1Kwj0VC2i9Owo43B%2fjkuWMm58zlWKIAL%2f1SEUCs6pOKlfkA4Lzij3jnYqijKydOwvX3te2vLBVSVp3LUX6eZB4HIs0NU2w0kXE2uP3gTKPjaMLT39CAV%2fv83Ae0fhbdCb%2bT82J4QAAA%3d%3d");
+
public static void main(String[] args) throws InterruptedException {
+ /**
+ * 1. Set up a string with the path to the website.
+ * 2. Create a desktop variable. The desktop class uses the computer's default browser to open the URL.
+ * 3. Load the URL.
+ * 4. Browse to the URL.
+ */
+
+ //TODO put your own clientId here.
+ String clientId = ;
+ String scopes = "wl.messenger";
+ String signInUrl = "https://oauth.live.com/authorize?client_id=" + clientId + "&redirect_uri=https://oauth.live.com/desktop&response_type=token&scope=" + scopes;
+
try {
- XmppClient client = new XmppClient(Program.TestAccessToken);
+ // launch a web browser to take the user through the OAuth 2.0 consent flow
+ BareBonesBrowserLaunch.browse(signInUrl);
+
+ // pop a dialog that tells the developer to copy and paste the URL and put it into a text box in the dialog
+ String returnUrlString = (String)JOptionPane.showInputDialog("After completing the OAuth consent flow in the browser, copy and paste the return URL into this dialog box");
+
+ // take the string URL from the dialog and programmatically cram it into the access token parameter
+ String accessToken = urlTokenizerHelper(returnUrlString);
+
+ // log in using the access token
+ XmppClient client = new XmppClient(accessToken);
client.logIn();
+
+ // make sure the program hasn't already closed before the login has completed
+ // in a real XMPP client, this would be replaced with waiting on UI events and Xmpp events
Thread.sleep(1000000);
} catch (Exception e) {
System.out.println(e);
}
}
+
+ /**
+ * This function helps to extract the access_token query string parameter from the return URL.
+ */
+ public static String urlTokenizerHelper(String urlString) {
+ URL returnUrl = null;
+ try{
+ returnUrl = new URL(urlString);
+ }catch(Exception e){}
+ String queryParameters = returnUrl.getRef();
+ queryParameters = queryParameters.substring(queryParameters.indexOf("access_token"));
+ queryParameters = queryParameters.substring(queryParameters.indexOf("=")+1);
+
+ String encodedAccessToken = queryParameters.substring(0, queryParameters.indexOf("&"));
+
+ return URLDecoder.decode(encodedAccessToken);
+
+ }
}
@@ -1,6 +1,5 @@
package com.ms.wlm;
-import java.net.URLDecoder;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;

0 comments on commit ac0d4aa

Please sign in to comment.