MySpace Developer Platform

A Place For Developers
From MySpace Open Platform: Documentation Wiki

MySpaceID PHP SDK Walkthrough for Relying Parties

Jump to: navigation, search
Content > MySpaceID > MySpaceID SDK > MySpaceID SDK for PHP


OpenID and OAuth have been around for awhile, but now with an approach we call 'Hybrid' we are able to blend the best of both worlds.

For those of you not familiar with OpenID, OpenID allows users to log into other Web sites with their login credentials from Web sites that support OpenID. Web sites such as MySpace that allow their users to bring a user's identity with them are called OpenID Providers (OPs). In this case, MySpace is proud to announce that we are an OpenID Provider, and we call our specific version of OpenID MySpaceID. MySpaceID follows all of the specifications and conventions of OpenID and OAuth to provide some extended functionality that OpenID does not do alone. MySpaceID will allow users to connect to your site in unprecedented ways, currently a new user to your site could use their MySpaceID to import their friends, profile, pictures, or the videos they have uploaded to MySpace. In the near future you will be able to use the MySpaceID SDK to publish data back into MySpace. As the SDK matures, you will see more of this behavior exposed.

Since we cannot just give Web sites carte blanche rights to a user's account to post or to get information, we use OAuth as a way to authenticate that a user gave your site privileges to access the on their behalf, allowing you access to their social graph and profile. When you combine OpenID and OAuth, a Relying Party based Web site can take full advantage of MySpace's Social Graph.

Contents

Overview

This walkthrough will cover setting up the 'MySpaceID-OpenID-OAuth' sample with your own consumer key and some brief discussion on some of the talking points of the code. This is a very basic sample meant to show a limited set of functionality and to get you up and running quickly. Sample suggestions are welcome.

  1. Creating an App:Getting a Consumer Key and Secret
  2. Getting the Source from Google Code's SVN
  3. Editing the config.MySpace.php & common.php files
  4. Logging in for the first time.

Server Requirements

  • Apache 2.0.63
  • PHP 5.2 +
    • SimpleXML 1.151.2.2.22.2.39
    • cURL 7.13.2
    • php.ini with the following settings
      • html_errors = On


This was tested on Dreamhost.com

SDK Install Walkthrough

Step 1: Set Up a New MySpaceID Application

Getting a Consumer Key and Consumer Secret from MySpace.com

Register the MySpaceID Application

  1. Go to http://developer.myspace.com
  2. Click the Build button in the navigation bar.
    Image:helloworld_step1-2a.png

  3. Click the Create MySpaceID App button located under the Application Platform logo as shown in the screenshot.
    Image:MySpaceID_step1-2.png‎

  4. Fill in the form:
    1. Enter the Application Title.
    2. Type the sequence of numbers and letters to solve the CAPTCHA challenge that appears on the screen.
    3. Click on the checkbox to agree to the Developer Addendum to MySpace Terms of Use Agreement.

      Note: You are using another users data so be sure to understand what you are responsible for. You must do this for each application you create.

    4. Click on the Next button.

    Image:MySpaceID_step1-3.png‎

Provide Information About the Application

The Edit App Information page appears. You can change the Application Title from this page at any time.

  1. Enter a short description of the application in the Application Description field (required).
  2. Enter an Uninstall Callback URL (optional). This URL is called when a user revokes an OAuth Token, or uninstalls the application.
  3. Enter the External URL (required). This URL will match the base of your application when the User is logged out.
  4. Enter the External Callback Validation URL (required).

    For the external domain, enter the Protocol/Scheme in the first box and Domain in the second box. This must match the same information provided in the oauth_callback parameter when requesting session based authentication.

    Example: protocol = http, domain = www.myspace.com will match oauth_callback=http://myspace.com/mycallback

    Note: query strings are ignored and sub-domains are not used to validate.)

  5. Click Show OAuth Consumer Secret to fetch the consumer Secret from the server.
  6. The Terms of Service for using External Domain appears on the page.
    1. Read the Terms of Service.
    2. Click checkbox next to "I agree with the Terms Of Service."

    Image:MySpaceID_step3-2.png

  7. To use OpenID for your application (for this walkthrough, follow these steps):
    1. Click on the Use OpenID checkbox.
    2. Add your Relying Party Realms. A Realm is the base path to your application
      • Your Realm must match exactly.
      • Realms must be unique.

      Enter a set of realm patterns that represents the URL-spaces for all OpenID authentication requests for your app. The realm patterns specified here will also be presented to the end-user during authentication to give them an indication of the scope of the authentication request.

      Image:MySpaceID_step3-3.png

    In this example, http://demos.jdavid.net is our server, and on that server, we have created a directory to test the MySpaceID-SDK, called mypspaceid-sdk. This directory's base path is now http://demos.jdavid.net/myspaceid-sdk/.

    The SDK has 3 directories in it:

    • config
    • samples
    • source

    Since the Samples are in the samples directory, and each sample has it's own directory we end up with a Realm for the OpenID + OAuth being:

    Examples of Sample Realms:

    • http://demos.jdavid.net/myspaceid-sdk/samples/myspaceid-openid/
    • http://demos.jdavid.net/myspaceid-sdk/samples/myspaceid-openid-oauth/
    • http://demos.jdavid.net/myspaceid-sdk/samples/myspaceid-openid-oauth-popup/

    Note: http://demos.jdavid.net/myspaceid-sdk/samples/myspaceid-oauth/ is not needed in the Realm List. This is because the REALM field is needed for OpenID applications only.

    • Your realms will need to be application specific; we use the realm to look up your application in certain situations, so they must be globally unique. However, you are allowed to have more than one realm per app.
    • You may choose to have more than one Realm per app if you are testing in several different locations, or you are sharing the consumer key and secret amongst several applications.
    • Even though wild cards are allowed, it is Highly Recommended to not use wild cards in your REALM list.
    • Your Realm must match exactly. Trailing slashes count.

    Recommendation: You will probably want to edit your hosts file if you plan to debug locally so that it is easier to have a unique Realm endpoint for your application.

  8. Click on the Save button on the bottom of the page.
    Image:MySpaceID_step3-4.png

A SUCCESS message displays at the top of the page:

Image:MySpaceID_step3-success.png‎

Note: You must save to update your settings.


You now have a New Consumer Key and Consumer Secret with your Realms set up.

Step 2: Getting Source Code from Google Code

Google Code is a great site and I have had zero problems with their SVN implementation. I also like using two tools to make it easier on Windows. I recommend the following tools for Windows Vista:

Downloading SVN tools

Getting Latest Version

  • Use for the latest release:
    • http://myspaceid-php-sdk.googlecode.com/svn/trunk/
  • Use for the latest stable release:
    • http://myspaceid-php-sdk.googlecode.com/svn/trunk/ (at some point these will be different)

image:Step2_getlatest.png‎

Using Tortiose SVN

If you are using Tortoise SVN right click in the directory you want the SDK to download to and select SVN Checkout. Grab the SVN URL from: http://myspaceid-php-sdk.googlecode.com/svn/trunk/

image:Step2_SVNurl.png‎

Click 'OK' to continue.

image:Step2_Checkout.png‎


image:Step2_Checkout-2.png‎

You should now have a local copy of the SDK.

Step 3: Editing the config.MySpace.php & common.php files

config.MySpace.php (or config.MySpace.local.php)

1	<?php
2	
3		//please register at http://developer.myspace.com for a CONSUMERK_KEY
4		define('CONSUMER_KEY', 'NOT SET');
5	
6		//please register at http://developer.myspace.com for a CONSUMER_SECRET
7		define('CONSUMER_SECRET', 'NOT SET');
8	
9		/**
10	     * This is where the example will store its OpenID information.
11	     * You should change this path if you want the example store to be
12	     * created elsewhere.  After you're done playing with the example
13	     * script, you'll have to remove this directory manually.
14	     */
15		define('TEMP_STORE_PATH', "tmp/_php_consumer_test");
16	
17		/**
18		 * map the following CONST to a proper file for your opperatin system/ enviroment
19		 *
20		 * "source/Auth/OpenID/CryptUtil.php"
21		 *
22		 * define('Auth_OpenID_RAND_SOURCE', 'C:\_net_capture\001.pcap');
23		 */
24	?>



Setting the Conumser Key & Secret

You will need to set the following lines:

4	define('CONSUMER_KEY', 'NOT SET');

Set your consumer key for most of the samples. The popup sample currently is configured to use its own consumer key.

7	define('CONSUMER_SECRET', 'NOT SET');

Set your consumer secret for most of the samples. The popup sample currently is configured to use its own consumer secret.



Setting the Temp Store Path

15	define('TEMP_STORE_PATH', "tmp/_php_consumer_test");

This path defines were the SDK manages a few temp files like nonces and what not. To get this set up don't worry about it too much, you might want to set it to a relative path like tmp/_php_consumer_test You will need to make sure that this directory has been created and the SDK has write access to it.

If this is not set correctly you may see this Error:

Warning: mkdir() [function.mkdir]: No such file or directory in 
/home/.jamshid/user1056/demos.jdavid.net/myspaceid-sdk/samples/myspaceid-openid-oauth/common.php on line 75
Could not create the FileStore directory 'c:/tmp/_php_consumer_test'. Please check the effective permissions.


CryptUtil.php settings

17	/**
18		 * map the following CONST to a proper file for your opperatin system/ enviroment
19		 *
20		 * "source/Auth/OpenID/CryptUtil.php"
21		 *
22		 * define('Auth_OpenID_RAND_SOURCE', 'C:\_net_capture\001.pcap');
23		 */

The above lines refer to some potential edits you need to make to the source/Auth/OpenID/CryptUtil.php file, which has an OS dependency.

Right now it should be set to which will get a random string of text from the linux os environment.

define('Auth_OpenID_RAND_SOURCE', '/dev/urandom');// for linux



Setting Source and Config Paths for your Sample App

samples\myspaceid-openid-oauth\common.php The first ~20 lines of code in this file:

1	<?php
2	
3	define('LIB_PATH', 		"../../source/");
4	define('CONFIG_PATH', 	"../../config/");
5	define('LOCAL', false);
6	
7	
8	$path_extra = dirname(dirname(dirname(__FILE__)));
9	$path = ini_get('include_path');
10	$path = CONFIG_PATH . PATH_SEPARATOR
11			.	LIB_PATH . PATH_SEPARATOR
12			.	$path_extra . PATH_SEPARATOR
13			.	$path;
14	ini_set('include_path', $path);
15	
16	function displayError($message) {
17	    $error = $message;
18	    include 'index.php';
19	    exit(0);
20	}

Lines 3 & 4 define where the source and config files are for the Classes in the SDK, since these are configurable for each sample, you can choose to use a specific branch of the SDK, or a specific consumer key and secret.

Line 5, defines whether to fetch the local config or the web server config. I wanted an easy mechanism so I could test and debug locally and on a webserver with nearly the same settings.

The code in line 55 of common.php shows how one would use the local constant to determine which version of the configuration to load, feel free to get as crazy as you like, but I like ternary operators in code, and I find them very readable when you get used to them. The following line says if LOCAL is true, then load 'config.MySpace.local.php', if it is false, then load 'config.MySpace.php'.

55		require_once LOCAL ? "config.MySpace.local.php" : "config.MySpace.php";

Step 4: Logging In for the First Time

You should be able to login on your local server, here is a final checklist to run through.

  1. Installed apache
  2. Set up your virtual directory
  3. Installed cUrl
  4. Set up the proper Realm at http://developer.myspace.com
  5. Edited the config.MySpace.php, Common.php, and CryptUtil.php files

Load the index.php file. See the following link for an example: http://demos.jdavid.net/myspaceid-sdk/samples/myspaceid-openid-oauth/

image:Step4_load.png‎

Fill in the vanity URL with your MySpace URL.

The following login window will appear:

image:Step4_vanity.png‎

Log in to MySpace now.

The browser should be redirected back to something like this:

image:Step4_browser.png‎

You now have your first OpenID application working.

Troubleshooting

Possible error messages

Realm mismatch

This error sometimes occurs when you have a realm mismatch. Please check your application and realm list and make sure that they match.

Fatal error: Uncaught exception 'MySpaceException' with message 'This function requires an OAuth token' 
in /home/.jamshid/$$$/demos.jdavid.net/myspaceid-sdk/source/MySpaceID/myspace.php:571
Stack trace: #0 /home/.jamshid/$$$/demos.jdavid.net/myspaceid-sdk/source/MySpaceID/myspace.php(454)
: MySpace->requireToken()
#1 /home/.jamshid/$$$/demos.jdavid.net/myspaceid-sdk/samples/myspaceid-openid-oauth/finish_auth.php(47)
: MySpace->getAccessToken() 
#2 /home/.jamshid/$$$/demos.jdavid.net/myspaceid-sdk/samples/myspaceid-openid-oauth/finish_auth.php(66)
: run() 
#3 {main} thrown in/home/.jamshid/$$$/demos.jdavid.net/myspaceid-sdk/source/MySpaceID/myspace.php on line 571