Instagram API强制签名请求+无效的签名请求:签名不匹配

时间:2015-10-26 21:07:52

标签: c# api httprequest instagram instagram-api

我是Instagram API的新用户,我有一个客户需要在他的网站上显示最新媒体,但我正在我的个人Instagram帐户下进行测试。

  • 在“安全”标签下,[禁用隐式OAuth]& [已强制签名的请求]已经过检查。
  • 我尝试拨打的端点是:[users / {user-id} / media / recent]
  • 签名生成方法:

    string GenerateSignature(string endpoint, 
             Dictionary<string, string> parameters, string secret) {
    
        StringBuilder message = new StringBuilder(endpoint);
    
        foreach (var param in parameters.OrderBy(p => p.Key))
        {
            message.AppendFormat("|{0}={1}", param.Key, param.Value);
        }
    
        return HMACSHA256_Hash(secret, message.ToString());
    }
    
    string HMACSHA256_Hash(string secret, string message)
    {
        // Create a HMAC-SHA256 digest of the message using the secret key
        HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
        byte[] digest = hmac.ComputeHash(Encoding.UTF8.GetBytes(message.ToString()));
    
        // Return the digest as a hexstring to be used as a signature for the request
        return ByteArrayToString(digest);
    }
    
    string ByteArrayToString(byte[] array)
    {
        // Convert the bytes in the array to a lower-case hexstring
        return array.Aggregate(new StringBuilder(), (sb, b) => sb.Append(b.ToString("x2"))).ToString();
    }
    
  • 获取近期媒体:

    protected void GetUserDetails()
    {
        string instagram_Username = "";
        string instagram_UserID = "";
        string instagram_AccessToken = "";
        string instagram_ClientID = "";
        string instagram_ClientSecret = "";
    
        int count = 1;
    
        string url = String.Format(@"https://api.instagram.com/v1/users/{0}/media/recent?client_id={1}&count={2}", instagram_UserID, instagram_ClientID, count);
        string endPoint = String.Format("/users/{0}/media/recent", instagram_UserID);
    
        Dictionary<string, string> endPointParameters = new Dictionary<string, string>();
        endPointParameters.Add("client_id", instagram_ClientID);
        endPointParameters.Add("count", count.ToString());
    
        string signature = GenerateSignature(endPoint, endPointParameters, instagram_AccessToken);
        url = url + "&sig=" + signature;
    }
    
  • 调用网址:https://api.instagram.com/v1/users/ {user-id} / media / recent?client_id = {cliend_id}&amp; count = 1&amp; sig = 7c024d67c1000106686c412778dc3534614d43822f9e13554418c3d3d5386872

  • 回复:{&#34;代码&#34;:403,&#34; error_type&#34;:&#34; OAuthForbiddenException&#34;,&#34; error_message&#34;:&#34 ;签名请求无效:签名与&#34;}

  • 不匹配

另外;我可以知道访问令牌是否会过期或者我可以在我的所有呼叫中使用相同的呼叫,并注意每次呈现网站的主页时都会进行呼叫: - )

我很感激帮助!非常感谢

1 个答案:

答案 0 :(得分:1)

我错过了秘密应该是客户秘密。

  $this->load->library('email');
                $config['protocol']='smtp';
                $config['smtp_host']='your host';
                $config['smtp_port']='465';
                $config['smtp_timeout']='30';
                $config['smtp_user']='your mail id';
                $config['smtp_pass']='your password';
                $config['charset']='utf-8';
                $config['newline']="\r\n";
                $config['mailtype'] = 'html';
                $this->email->initialize($config); //intializing config array 
                $this->email->from('your@example.com', 'Site name');
                $this->email->to('someone@somewhere.com');
                $this->email->subject('Mail');
                $this->email->message('Your message');
                $this->email->send();