MySpace Developer Platform

A Place For Developers
From MySpace Open Platform: Documentation Wiki

MySpaceID JSL Example Usage

Jump to: navigation, search
Content
Content > Quick Start Guides > MySpaceID > MySpaceID JSL

Contents

Referencing the Library

Place this in the head of your html document

<script src="http://api.myspace.com/jsl/scriptrequest.ashx?type=JSL" type="text/javascript"></script>

While developing, you may find it useful to use the unminified version of library. To do so, use the following path instead

<script src="http://api.myspace.com/jsl/scriptrequest.ashx?type=JSL&mode=debug" type="text/javascript"></script>

Initializing the Library

Define the consumer key (the consumer key issued when you create an app at http://developer.myspace.com) and target domain (typically your site’s domain):

var myspaceSettings = {	
    consumerKey:'{consumerKey}', //replace with your own consumer key
    targetDomain: 'http://somedomain.com/'  //replace with your own domain, this will describe where the RelayReceiver.html file is located.
};

Initialize the library with the initial settings (only one MySpace library is allowed at a time):

//may change to MSID.init()
MSID.Connect.init(myspaceSettings);

Attempt to set up the connection to http://api.myspace.com and handle the response:

function handleResponse(response) {
    if (response.statusCode == MSID.Connect.Enums.status.READY) {
        alert('Ready');
    } else if (response.statusCode == MSID.Connect.Enums.status.COOKIE_NOT_FOUND) {
        alert('No Cookie (not logged in)';
    } else if (response.statusCode == MSID.Connect.Enums.status.LIBRARY_INIT_TIMEOUT) {
	alert('Timeout');
    } else {
        alert(response.statusCode);
    }
}	
 
MSID.Connect.tryConnect( handleResponse );

Logging In

<a class="msid__login" onclick="MSID.Connect.requestLogin(handleResponse, 3000)" href="#login">
    <img alt="Login with MySpaceID" src="myspaceid.png"/>
</a>

Posting an Activity to MySpace

function postActivity() {
    var params = {};
 
    params[opensocial.Activity.Field.TITLE_ID] = 'Template_Name';
 
    var template_params = ["paramKey1", "paramValue1", "paramKey2", "paramValue2"];
 
    params[opensocial.Activity.Field.TEMPLATE_PARAMS] = {};
    for (i = 0; i < template_params.length - 1; i++) {
        params[opensocial.Activity.Field.TEMPLATE_PARAMS][template_params[i]] = template_params[++i];
    }
 
    var mediaItemArray = [];
 
    var arr = ["mediaItemKey1", "http://myspacedemo.com/mediaItem1.jpg"];
 
    if (arr != '_') {
        for (i = 0; i < arr.length; i++) {
            mediaItemArray.push(MSID.newMediaItem(arr[i], arr[++i]));
        }
 
        params[opensocial.Activity.Field.MEDIA_ITEMS] = mediaItemArray;
    }
 
    var activity = MSID.newActivity(params);
 
    var pri = opensocial.CreateActivityPriority.HIGH;
    MSID.requestCreateActivity(activity, pri, postActivityResponse);
}
 
function postActivityResponse(response) {
    if (response.hadError()) {
        alert("requestCreateActivity failed.");
        return;
    }
    else {
        //activity posted successfully, handle appropriate to your app
    }
}

Making an OpenSocial Call

function fetchPerson() {
    var opt_params = {};
 
    opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.ABOUT_ME];
 
    os = MSID.Container.get();
    dataReqObj = os.newDataRequest();
    var viewerReq = os.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER);
    dataReqObj.add(viewerReq);
    dataReqObj.send(fetchPersonResponse);
}
 
function fetchPersonResponse(data) {
    // show error msg and error code            
    if (data.hadError()) {
        var thisErrorCode;
        var thisErrorMsg;
 
        if (data.get(MyOpenSpace.RequestType.FETCH_PERSON) != null) {
            thisErrorCode = data.get(MyOpenSpace.RequestType.FETCH_PERSON).getErrorCode();
            thisErrorMsg = data.get(MyOpenSpace.RequestType.FETCH_PERSON).getErrorMessage();
        }
        else {
            thisErrorCode = "Unknown ErrorCode!";
            thisErrorMsg = "Unknown ErrorMsg!";
        }
 
        alert('ErrorCode: ' + thisErrorCode + ' | ' + 'Error Message: ' + thisErrorMsg);
 
        return;
    }
    resultHtmlStr = "";
 
    var viewer = data.get(MyOpenSpace.RequestType.FETCH_PERSON)).getData();
    // basic
    var id = viewer.getField(opensocial.Person.Field.ID);
    var nickname = viewer.getField(opensocial.Person.Field.NICKNAME);
    var thumbnailUrl = viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
    var profileUrl = viewer.getField(opensocial.Person.Field.PROFILE_URL);
 
    // per request                                  
    var about_me = viewer.getField(opensocial.Person.Field.ABOUT_ME);
 
    alert(about_me);
}

Getting Activities via OpenSocial

function getFriendsActivities() {
    var id = new opensocial.IdSpec();
    id.setField(opensocial.IdSpec.Field.USER_ID, opensocial.IdSpec.PersonId.VIEWER);
    id.setField(opensocial.IdSpec.Field.NETWORK_DISTANCE, 1);
    id.setField(opensocial.IdSpec.Field.GROUP_ID, opensocial.IdSpec.GroupId.FRIENDS);
 
    var dataReq = MSID.newDataRequest();
    for (i = 0; i < idArray.length; i++) {
        dr.add(dr.newFetchActivitiesRequest(idArray[i]));
    }
    dr.send(getActivitiesResponse);
}
 
function getMyActivities() {
    var id = new opensocial.IdSpec();
    id.setField(opensocial.IdSpec.Field.USER_ID, opensocial.IdSpec.PersonId.VIEWER);
    id.setField(opensocial.IdSpec.Field.NETWORK_DISTANCE, 0);
 
    var dataReq = MSID.newDataRequest();    
    dataReq.add(dr.newFetchActivitiesRequest(id));
    dataReq.send(getActivitiesResponse);
}
 
function getActivitiesResponse(response) {
    if (response.hadError()) {
        alert("fetchActivitiesRequest failed.");
        return;
    }
 
    var activities = response.get(MyOpenSpace.RequestType.FETCH_ACTIVITIES).getData();
    for(var act in activities)
        visualizeActivity(act);                   
}
 
function visualizeActivity(act) 
{
    var appId = act.getField(opensocial.Activity.Field.APP_ID)
    var body = act.getField(opensocial.Activity.Field.BODY);
    var postTime = act.getField(opensocial.Activity.Field.POSTED_TIME);
    var streamFaviconUrl = act.getField(opensocial.Activity.Field.STREAM_FAVICON_URL);
    var title = act.getField(opensocial.Activity.Field.TITLE);
    var titleId = act.getField(opensocial.Activity.Field.TITLE_ID);
    var userId = act.getField(opensocial.Activity.Field.USER_ID);
      alert('title: ' + title + ' body: ' + body + ' @' + postTime);
}