I did it before and worked for me. Have a look at my implementation for
OAuth thru 2 legged.

package com.cisco.opensocial;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.opensocial.models.Activity;
import org.opensocial.models.AppData;
import org.opensocial.models.Person;
import org.opensocial.providers.MySpaceProvider;
import org.opensocial.providers.Provider;
import org.opensocial.providers.ShindigProvider;
import org.opensocial.services.ActivitiesService;
import org.opensocial.services.AppDataService;
import org.opensocial.services.PeopleService;
import org.opensocial.auth.AuthScheme;
import org.opensocial.auth.OAuth2LeggedScheme;
import org.opensocial.auth.SecurityTokenScheme;
import org.opensocial.Client;
import org.opensocial.Request;
import org.opensocial.Response;

@SuppressWarnings("unused")
public class LocalOpenSocialContainerTest {
    //private static final String TOKEN =
"U-10495:U-10493:10507:1:localhost%3A8080%2Fopensocial%2Ftest.xml:1:10171";
    //default to the appId
http://localhost:8080/gadgets/files/samplecontainer/examples/SocialActivitiesWorld.xml
    private static final String CONSUMER_KEY = "10507";
    private static final String CONSUMER_SECRET = "secret";
    private static final String VIEWER_ID = "U-10493";
    private Provider provider = new ShindigProvider(true);
    //private AuthScheme scheme = new SecurityTokenScheme(TOKEN);
    private AuthScheme scheme = new OAuth2LeggedScheme(CONSUMER_KEY,
CONSUMER_SECRET,VIEWER_ID);
    Client client = new Client(provider, scheme);

    public void createActivity() {
        Activity activity = new Activity();
        activity.setTitle("opensocial-java-client test activity");
        activity.setBody("opensocial-java-client test activity body at "+new
Date().getTime());
        activity.setTitleId("test");
        try {
             //client = new Client(provider, scheme);
             Request request = ActivitiesService.createActivity(activity);
             client.send(request);
             System.out.println("Successfully created activity! ");
        } catch (Exception e) {
          System.out.println("Exception occurred while processing request");
        }
      }
    public void fetchActivity() {
        // Request a specific user's activities
        Request userActivities = ActivitiesService.getActivities(VIEWER_ID);
        try {
        Response response = client.send(userActivities);
            List<Activity> activities = response.getEntries();
            for (Activity activity : activities) {
                  // Process an individual activity
                System.out.println(activity.getTitle()+" :
"+activity.getBody());
                }
                 System.out.println("Successfully fetched activities! ");
        } catch (Exception e) {
                System.out.println("Exception occurred while processing
request");
        }
      }

    public void fetchPeople() {
        // Request a specific user's friends
        // Request a specific user's friends
        Request userFriends = PeopleService.getFriends("U-10315");
        try {
            Response response = client.send(userFriends);
            List<Person> friends = response.getEntries();
            for (Person friend : friends) {
                  // Process an individual activity
                System.out.println(friend.getDisplayName()+" :
"+friend.getId());
                }
                 System.out.println("Successfully fetched user friends! ");
        } catch (Exception e) {
                System.out.println("Exception occurred while processing
request");
        }
      }
    public void updateAppData() {
        try {
            Map<String, String> data = new HashMap<String, String>();
            data.put("key1", "value1");
            data.put("key2", "value2");
            Request request = AppDataService.updateAppData(data);
            client.send(request);
            System.out.println("Successfully updated user appdata! ");
        } catch (Exception e) {
            System.out.println("Exception occurred while processing
request");
        }
      }

    public void getAppData() {
        try {
                Request request = AppDataService.getAppData();
                Response response = client.send(request);
                AppData data = response.getEntry();
                if(data != null)
                System.out.println(data.getDataForUser(VIEWER_ID, "key1"));
                System.out.println("Successfully retrieved user appdata! ");
        } catch (Exception e) {
                System.out.println("Exception occurred while processing
request");
        }
      }

    public static void main(String arg[]){
        LocalOpenSocialContainerTest test = new
LocalOpenSocialContainerTest();
        //test.createActivity();
        //test.fetchActivity();
        test.fetchPeople();
        //test.updateAppData();
        //test.getAppData();
    }
}

Let me know, if you need more explanation.

Thanks
raj

On Thu, Sep 23, 2010 at 2:27 PM, SteveM <[email protected]> wrote:

> Raj,
>
> Thanks for responding, but I don't understand your response. I'm using
> the oauth.googlecode.com Java client libraries today for 3-legged
> OAuth and those work fine. My code looks something like this:
>
> final CustomOAuthService garminOAuthService =
> CustomOAuthService.getInstance();
>        final String serviceURL = getEndPoint() + "json/" + noun;
>        final CustomOAuthConsumer consumer =
> customOAuthService.getCustomOAuthConsumer();
>        final OAuthAccessor accessor = new OAuthAccessor(consumer);
>        if (accessToken != null) {
>            accessor.accessToken = accessToken.getToken();
>            accessor.tokenSecret = accessToken.getTokenSecret();
>        }
>        Set<Map.Entry> entrySet = null;
>        if (params != null) {
>            entrySet = params.entrySet();
>        }
>        OAuthMessage result = CLIENT.invoke(accessor, method,
> serviceURL, entrySet);
>
> From what I can see the OAuthAccessor is only capable of doing 3-
> legged OAuth. I'm looking for and example of how to use the
> oauth.googlecode.com Java libraries to do 2-legged AOuth. If I saw a
> working example I could adapt it to my uses.
>
> Thanks,
>
> Steve
>
> On Sep 23, 4:15 pm, Rajender Reddy <[email protected]> wrote:
> > use java client libraries
> >
> > On Thu, Sep 16, 2010 at 2:03 PM, SteveM <[email protected]>
> wrote:
> > > Where might I find an example of doing 2-legged OAuth using the Java
> > > implementation.
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "OAuth" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<oauth%[email protected]><
> oauth%[email protected]<oauth%[email protected]>
> >.
> > > For more options, visit this group at
> > >http://groups.google.com/group/oauth?hl=en.
> >
> > --
> > Thanks,
> > -Raj
>
> --
> You received this message because you are subscribed to the Google Groups
> "OAuth" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected] <oauth%[email protected]>.
> For more options, visit this group at
> http://groups.google.com/group/oauth?hl=en.
>
>


-- 
Thanks,
-Raj

-- 
You received this message because you are subscribed to the Google Groups 
"OAuth" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/oauth?hl=en.

Reply via email to