コード例 #1
0
ファイル: MailRu.cs プロジェクト: DreamerUA/nemiro.oauth.dll
    public ActionResult MailRu(string method)
    {
      try
      {
        if (Session["Mail.Ru:AccessToken"] == null)
        {
          throw new Exception(Test.Resources.Strings.SessionIsDead);
        }

        // get access token from session
        var token = (OAuth2AccessToken)Session["Mail.Ru:AccessToken"];

        // query parameters
        var parameters = new NameValueCollection
        { 
          { "method", method },
          { "app_id", ConfigurationManager.AppSettings["oauth:mail.ru:id"] },
          { "uid" , Session["Mail.Ru:UserId"].ToString() },
          { "secure", "1" },
          { "format", "json" }
        };

        string signatureBaseString = parameters.Sort().ToParametersString();
        parameters["sig"] = OAuthUtility.GetMD5Hash(signatureBaseString + ConfigurationManager.AppSettings["oauth:mail.ru:key"]);

        // execute the request
        var result = OAuthUtility.ExecuteRequest("POST", "http://www.appsmail.ru/platform/api", parameters);

        return Content(result.ToString(), "text/plain");
      }
      catch (Exception ex)
      {
        return Content(ex.ToString(), "text/plain");
      }
    }
コード例 #2
0
    /// <summary>
    /// Gets base string of the signature for current request (OAuth 1.0).
    /// </summary>
    /// <remarks><para>For more details, please visit <see href="http://tools.ietf.org/html/rfc5849#section-3.4.1.1"/></para></remarks>
    public static string GetSignatureBaseString(string httpMethod, Uri url, NameValueCollection parameters, OAuthAuthorization authorization)
    {
      if (String.IsNullOrEmpty(httpMethod)) { throw new ArgumentNullException("httpMethod"); }
      if (authorization == null) { throw new ArgumentNullException("authorization"); }
      if (url == null) { throw new ArgumentNullException("url"); }

      var param = new NameValueCollection();
      if (parameters != null) { param.Add(parameters); }

      // append the authorization headers
      foreach (KeyValuePair<string, UniValue> itm in authorization.Value.CollectionItems)
      {
        if (itm.Key.Equals("oauth_signature", StringComparison.OrdinalIgnoreCase)) { continue; }
        param.Add(itm.Key, itm.Value.ToString());
      }

      // append the query parameters
      string queryString = url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
      if (!String.IsNullOrEmpty(queryString))
      {
        foreach (string q in queryString.Split('&'))
        {
          string[] p = q.Split('=');
          string key = p.First(), value = (p.Length > 1 ? p.Last() : "");
          param.Add(key, value);
        }
      }

      // sorting and build base string of the signature
      StringBuilder signBaseString = new StringBuilder();
      foreach (var itm in param.Sort().ToKeyValuePairCollection())
      {
        //if (itm.Key.Equals("oauth_verifier", StringComparison.OrdinalIgnoreCase)) { continue; }
        if (signBaseString.Length > 0) { signBaseString.Append(OAuthUtility.UrlEncode("&")); }
        signBaseString.Append(OAuthUtility.UrlEncode(String.Format("{0}={1}", itm.Key, OAuthUtility.UrlEncode(itm.Value))));
      }

      signBaseString.Insert(0, String.Format("{0}&{1}&", httpMethod.ToUpper(), OAuthUtility.UrlEncode(url.ToString())));

      return signBaseString.ToString();
    }
コード例 #3
0
    /// <summary>
    /// Gets the user details.
    /// </summary>
    /// <param name="accessToken">May contain an access token, which will have to be used in obtaining information about the user.</param>
    /// <exception cref="ApiException"/>
    public override UserInfo GetUserInfo(AccessToken accessToken = null)
    {
      // http://apiok.ru/wiki/pages/viewpage.action?pageId=46137373#APIДокументация(Русский)-users.getCurrentUser

      accessToken = base.GetSpecifiedTokenOrCurrent(accessToken);

      // query parameters
      var parameters = new NameValueCollection
      { 
        { "method", "users.getCurrentUser" },
        { "application_key", this.ApplicationKey },
        { "format", "json" },
        { "fields", "uid,first_name,last_name,name,gender,birthday,location,pic640x480,email" }
      };

      // signature base string
      // http://apiok.ru/wiki/pages/viewpage.action?pageId=75989046
      string signatureBaseString = parameters.Sort().ToParametersString(true);
      signatureBaseString += OAuthUtility.GetMD5Hash(accessToken.Value + this.ApplicationSecret);

      // calculate the signature
      parameters["sig"] = OAuthUtility.GetMD5Hash(signatureBaseString);
      parameters["access_token"] = accessToken.Value;

      // execute the request
      var result = OAuthUtility.Post("http://api.odnoklassniki.ru/fb.do", parameters);

      // error result
      if (result["error_code"].HasValue)
      {
        throw new ApiException
        (
          result,
          result.ContainsKey("error_msg") ? result["error_msg"].ToString() : null
        );
      }

      // successfully
      // field mapping
      var map = new ApiDataMapping();
      map.Add("uid", "UserId", typeof(string));
      map.Add("first_name", "FirstName");
      map.Add("last_name", "LastName");
      map.Add("name", "DisplayName");
      map.Add("email", "Email");
      map.Add("email", "UserName");
      map.Add("pic640x480", "Userpic");
      map.Add("link", "Url");
      map.Add("birthday", "Birthday", typeof(DateTime), @"yyyy\-MM\-dd");
      map.Add
      (
        "gender", "Sex",
        delegate(UniValue value)
        {
          if (value.Equals("male", StringComparison.OrdinalIgnoreCase))
          {
            return Sex.Male;
          }
          else if (value.Equals("female", StringComparison.OrdinalIgnoreCase))
          {
            return Sex.Female;
          }
          return Sex.None;
        }
      );

      return new UserInfo(result, map);
    }
コード例 #4
0
    /// <summary>
    /// Gets the user details.
    /// </summary>
    /// <param name="accessToken">May contain an access token, which will have to be used in obtaining information about the user.</param>
    /// <returns>
    /// <para>Returns an instance of the <see cref="UserInfo"/> class, containing information about the user.</para>
    /// </returns>
    /// <remarks>
    /// <para>The access token must contain the user ID in the parameter <b>x_mailru_vid</b>.</para>
    /// </remarks>
    /// <exception cref="ApiException"/>
    public override UserInfo GetUserInfo(AccessToken accessToken = null)
    {
      // http://api.mail.ru/docs/reference/rest/users.getInfo/

      accessToken = base.GetSpecifiedTokenOrCurrent(accessToken);

      var parameters = new NameValueCollection
      { 
        { "method", "users.getInfo" },
        { "app_id", this.ApplicationId },
        { "secure", "1" },
        { "uid", accessToken["x_mailru_vid"].ToString() },
        { "format", "json" }
      };

      string signatureBaseString = parameters.Sort().ToParametersString();

      parameters["sig"] = OAuthUtility.GetMD5Hash(signatureBaseString + this.ApplicationSecret);

      var result = OAuthUtility.Post("http://www.appsmail.ru/platform/api", parameters);

      if (!result.IsCollection)
      {
        throw new ApiException(result, "Expected one-dimensional array."); // expected one-dimensional array.
      }

      var map = new ApiDataMapping();
      map.Add("uid", "UserId", typeof(string));
      map.Add("first_name", "FirstName");
      map.Add("last_name", "LastName");
      map.Add("nick", "DisplayName");
      map.Add("email", "Email");
      map.Add("email", "UserName");
      map.Add("pic", "Userpic");
      map.Add("link", "Url");
      map.Add("birthday", "Birthday", typeof(DateTime), @"dd\.MM\.yyyy");
      map.Add
      (
        "sex", "Sex",
        delegate(UniValue value)
        {
          if (Convert.ToInt32(value) == 0)
          {
            return Sex.Male;
          }
          else if (Convert.ToInt32(value) == 1)
          {
            return Sex.Female;
          }
          return Sex.None;
        }
      );

      return new UserInfo(result.First(), map);
    }