0

Session Management Implement by C#

Hi all,

I read http://developer.yahoo.com/messenger/guide/ch01s04.html, but poor me, I can't understand how to implement the request and response for Session Management in C#.

Please show me how to work with it.

Thanks.

by
9 Replies
  • Hi

    1. You need to understand how does http work.
    2. You need to understand how to access URL in C#.net
    3. Then you can play with YM session management

    Thx
    ~jimmi
    0
  • QUOTE (Jimmi K @ Aug 31 2010, 08:47 PM) <{POST_SNAPBACK}>
    Hi

    1. You need to understand how does http work.
    2. You need to understand how to access URL in C#.net
    3. Then you can play with YM session management

    Thx
    ~jimmi

    Jimmi, I've got the same problem, and I understand your 1st and 2nd steps. But please elaborate step 3. is there a particular URL to query for creating a session?? I've got the following code which returns unauthorized from the server.
    CODE
                    string uri = "http://developer.messenger.yahooapis.com/v1/session";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.ContentType = JSONContentType;
    request.Host = "developer.messenger.yahooapis.com";

    request.Headers.Add(HttpRequestHeader.Authorization, Request.Cookies["oauth_token"].Value);
    string session_data = "{}";
    //request.ContentLength = session_data.Length;
    byte[] array = Encoding.UTF8.GetBytes(session_data);
    request.GetRequestStream().Write(array, 0, array.Length);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string data = ReadResponse(response);
    Response.Write("<br/><font color=red>Returned data:</font>" + data + "<br/>");
    0
  • You should put OAuth token in the url parameter, and set content-type to application/json; charset=utf-8

    Here is the sample code:
    CODE
     Random random = new Random();
    Hashtable access = (Hashtable) _token["access"];

    string url = "http://developer.messenger.yahooapis.com/v1/session" +
    "?oauth_consumer_key=" + _config["consumer_key"] +
    "&oauth_nonce=" + random.Next(10000000, 99999999) +
    "&oauth_signature=" + _config["secret_key"] + "%26" + access["oauth_token_secret"] +
    "&oauth_signature_method=PLAINTEXT" +
    "&oauth_timestamp=" + getTimestamp() +
    "&oauth_token=" + access["oauth_token"] +
    "&oauth_version=1.0" +
    "&notifyServerToken=1";

    string postdata = "{\"presenceState\" : " + state + ", \"presenceMessage\" : \"" + status + "\"}";
    string rs = fetchURL(url, true, postdata);


    CODE
    public string fetchURL(string url, bool json, string postData)
    {
    _error = "";

    StringBuilder sb = new StringBuilder();
    byte[] buf = new byte[8192];

    try
    {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    if (json)
    {
    request.ContentType = "application/json; charset=utf-8";
    }
    if (postData != "")
    {
    request.Method = "POST";
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = encoding.GetBytes(postData);
    request.ContentLength = data.Length;

    Stream newStream = request.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();

    }

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream resStream = response.GetResponseStream();
    string tempString = null;
    int count = 0;
    do
    {
    count = resStream.Read(buf, 0, buf.Length);
    if (count != 0)
    {
    tempString = Encoding.ASCII.GetString(buf, 0, count);
    sb.Append(tempString);
    }
    }
    while (count > 0);
    }
    catch (WebException e)
    {
    _error = e.Message;
    return "";
    }

    return sb.ToString();
    }

    private string getTimestamp()
    {
    double t = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    return t.ToString();
    }


    Thx
    ~jimmi
    0
  • Thanks a lot for the code. I followed it but still get unauthorized error. Captured HTTP packet says Yahoo XML error is "Please provide valid credentials. OAuth oauth_problem="token_rejected", realm="yahooapis.com""

    Here is what my begining part of my request looks like (rest of the part omitted):

    http://developer.messenger.yahooapis.com/v...i9R5kT.SADgl7Rq

    Any Ideas??
    0
  • Hi Sandy

    You should change oauth_token parameter, in your link you wrote:
    oauth_token=A=znZuILbyvCDoxHLudGccoLw3am4HmZLCxICbgPJ6iQXCbRQHrJXg5z7IgT_J8cMaj8
    vNkwOameP129bxkhai88VeNw9ml08tK.0VTMlKzo2UnsQ7ban4sh6qTclvk9YhtUxtUxtX...

    it should be:
    oauth_token=A%3DznZuILbyvCDoxHLudGccoLw3am4HmZLCxICbgPJ6iQXCbRQHrJXg5z7IgT_J8cMaj8vNkwOameP129
    bxkhai88VeNw9ml08tK.0VTMlKzo2UnsQ7ban4sh6qTclvk9YhtUxtUxtX...

    Thx
    ~jimmi
    0
  • QUOTE (Jimmi Test @ Sep 8 2010, 12:03 AM) <{POST_SNAPBACK}>
    You should put OAuth token in the url parameter, and set content-type to application/json; charset=utf-8

    Here is the sample code:
    CODE
     Random random = new Random();
    Hashtable access = (Hashtable) _token["access"];

    string url = "http://developer.messenger.yahooapis.com/v1/session" +
    "?oauth_consumer_key=" + _config["consumer_key"] +
    "&oauth_nonce=" + random.Next(10000000, 99999999) +
    "&oauth_signature=" + _config["secret_key"] + "%26" + access["oauth_token_secret"] +
    "&oauth_signature_method=PLAINTEXT" +
    "&oauth_timestamp=" + getTimestamp() +
    "&oauth_token=" + access["oauth_token"] +
    "&oauth_version=1.0" +
    "&notifyServerToken=1";

    string postdata = "{\"presenceState\" : " + state + ", \"presenceMessage\" : \"" + status + "\"}";
    string rs = fetchURL(url, true, postdata);


    CODE
    public string fetchURL(string url, bool json, string postData)
    {
    _error = "";

    StringBuilder sb = new StringBuilder();
    byte[] buf = new byte[8192];

    try
    {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    if (json)
    {
    request.ContentType = "application/json; charset=utf-8";
    }
    if (postData != "")
    {
    request.Method = "POST";
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = encoding.GetBytes(postData);
    request.ContentLength = data.Length;

    Stream newStream = request.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();

    }

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream resStream = response.GetResponseStream();
    string tempString = null;
    int count = 0;
    do
    {
    count = resStream.Read(buf, 0, buf.Length);
    if (count != 0)
    {
    tempString = Encoding.ASCII.GetString(buf, 0, count);
    sb.Append(tempString);
    }
    }
    while (count > 0);
    }
    catch (WebException e)
    {
    _error = e.Message;
    return "";
    }

    return sb.ToString();
    }

    private string getTimestamp()
    {
    double t = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    return t.ToString();
    }


    Thx
    ~jimmi


    Hello,thank you for your help!
    I take these code in my program. But when compilying the code, it indicates that :
    you have not definde “_token”and “_config”;
    How can I solve this problem?
    thank you!
    0
  • Where do i get the value for this parameter oauth_token from?
    0
  • QUOTE (Oluwadamilola @ Sep 21 2010, 06:08 AM) <{POST_SNAPBACK}>
    Where do i get the value for this parameter oauth_token from?


    Still require assistance.
    0
  • First thing you need to do is sending user name and password, read this

    thx
    ~jimmi
    0

Recent Posts

in Messenger IM SDK