2015-01-21 66 views
0

我正在向instagram api做webrequest,並且收到400個不好的請求,但我想這不是instagram專用的,並且是我的代碼中的一個通用錯誤。 這裏是我的代碼 「代碼」 在該請求參數是通過以前的request.Code處斷裂 「的GetResponse」 方法WebRequest 400對api的不良要求

string clientID = "myclientid"; 
    string clientsecret="myclientsecret"; 
    string redirectURL = "http://localhost:64341/"; 
string webrequestUri = "https://api.instagram.com/oauth/ 
    access_token? client_id=" + clientID + "&client_secret= 
    " + clientsecret + "&grant_type=authorization_code"           + "&redirect_uri="redirectURL+"&code=" 
+Request.QueryString["code"].ToString(); 
WebRequest request = WebRequest.Create(webrequestUri); 
request.Method = "POST"; 
WebResponse response = request.GetResponse(); 
+0

您是否嘗試過在看什麼有效的請求樣子? – 2015-01-21 07:18:07

+0

的Instagram文檔說 捲曲\ -F '= CLIENT_ID CLIENT-ID' \ -F '= client_secret CLIENT-SECRET' \ -F '= grant_type authorization_code' \ -F '= REDIRECT_URI YOUR-REDIRECT-URI' \ -F'code = CODE'\ https://api.instagram.com/oauth/access_token 我不是一個不幸的PHP傢伙。 – 2015-01-21 07:24:42

+0

這是正確的嗎? 「&grant_type = authorization_code」+「&redirect_uri =」redirectURL +「&code =」 + Request.QueryString [「code」]。ToString(); – Amit 2015-01-21 07:25:58

回答

1

C#/ asp.net工作示例獲得:

namespace InstagramLogin.Code 
{ 
    public class InstagramAuth 
    { 
     private InstagramClient myApp = new InstagramClient(); 

     public string genUserAutorizationUrl() 
     { 
      return String.Format(myApp.OAuthAutorizeURL, myApp.ClientId, myApp.RedirectUri); 
     } 

     public AuthUserApiToken getTokenId(string CODE) 
     { 
      //curl \-F 'client_id=CLIENT-ID' \ 
      //-F 'client_secret=CLIENT-SECRET' \ 
      //-F 'grant_type=authorization_code' \ 
      //-F 'redirect_uri=YOUR-REDIRECT-URI' \ 
      //-F 'code=CODE' \https://api.instagram.com/oauth/access_token 

      var wc = new WebClient(); 
      var wcResponse = wc.UploadValues(myApp.AuthAccessTokenUrl, 
           new System.Collections.Specialized.NameValueCollection() { 
            { "client_id", myApp.ClientId }, 
            { "client_secret", myApp.ClientSecret }, 
            { "grant_type", "authorization_code"}, 
            { "redirect_uri", myApp.RedirectUri}, 
            { "code", CODE} 
           }); 
      var decodedResponse = wc.Encoding.GetString(wcResponse); 
      AuthUserApiToken UserApiToken = JsonConvert.DeserializeObject<AuthUserApiToken>(decodedResponse); 

      return UserApiToken; 
     } 
    } 
} 

你的對象:

namespace InstagramLogin.Code 
    { 
    public class InstagramClient 
     { 

      private const string ApiUriDefault = "https://api.instagram.com/v1/"; 
      private const string OAuthUriDefault = "https://api.instagram.com/oauth/"; 
      private const string RealTimeApiDefault = "https://api.instagram.com/v1/subscriptions/"; 

      private const string OAuthAutorizeURLDefault = "https://api.instagram.com/oauth/authorize/?client_id={0}&redirect_uri={1}&response_type=code"; 
      private const string AuthAccessTokenUrlDefault = "https://api.instagram.com/oauth/access_token"; 

      private const string clientId = "clientid"; 
      private const string clientSecretId = "clientsecretid"; 
      private const string redirectUri = "http://localhost/InstagramLogin/InstaAuth.aspx"; 
      private const string websiteUri = "http://localhost/InstagramLogin/InstaAuth.aspx"; 

      public string ApiUri { get; set; } 
      public string OAuthUri { get; set; } 
      public string RealTimeApi { get; set; } 


      public string OAuthAutorizeURL { get { return OAuthAutorizeURLDefault; } } 
      public string ClientId { get { return clientId; } } 
      public string ClientSecret { get { return clientSecretId; } } 
      public string RedirectUri { get { return redirectUri; } } 
      public string AuthAccessTokenUrl { get { return AuthAccessTokenUrlDefault; } } 
//removed props 
    } 
    } 

的Instagram loged用戶:

namespace InstagramLogin.Code 
{ 
    public class SessionInstaAuthUser 
    { 

      public bool isInSession() 
      { 
       return HttpContext.Current.Session["AuthUserApiToken"] != null; 
      } 

      public AuthUserApiToken get() 
      { 
       if (isInSession()) 
       { 
        return HttpContext.Current.Session["AuthUserApiToken"] as AuthUserApiToken; 
       } 
       return null; 
      } 

      public void set(AuthUserApiToken value) 
      { 
       HttpContext.Current.Session["AuthUserApiToken"] = value; 
      } 

      public void clear() 
      { 
       if (isInSession()) 
       { 
        HttpContext.Current.Session["AuthUserApiToken"] = null; 
       } 
      } 

    } 
} 

標記:

<asp:Button ID="btnInstaAuth" 
     Text="Click here to get instagram auth" 
     runat="server" /> 

後面的代碼:

//objects 
private InstagramClient InstagramApiCLient = new InstagramClient(); 
     private InstagramAuth AuthManager = new InstagramAuth(); 
     private SessionInstaAuthUser SesssionInstaUser = new SessionInstaAuthUser(); 

//點擊與測試登錄 - 用戶登錄(會話)

void btnInstaAuth_Click(Object sender, 
           EventArgs e) 
     { 
      btnGetInstaPosts.Visible = false; 
      if (SesssionInstaUser.isInSession()) 
      { 
       SesssionInstaUser.clear(); 
       Button clickedButton = (Button)sender; 
       clickedButton.Text = "Click here to get instagram auth"; 
       btnInstaAuth.Visible = true; 
      } 
      else 
      { 
       Response.Redirect(AuthManager.genUserAutorizationUrl()); 
      } 
     } 

你可以找到你需要的東西這個類InstagramAuth對不起,如果我忘記刪除一些額外的方法或屬性,請將其去掉。

此按鈕可以在所有網頁上使用,仍然沒有忘記在Instagram的作爲登錄頁面,查詢字符串解析設置頁面在頁面加載補充:

//here you read the token instagram generated and append it to the session, or get the error :) 

    if (!IsPostBack) 
       { 
        if (!SesssionInstaUser.isInSession()) 
        { 
         if (Request.QueryString["code"] != null) 
         { 


          var token = AuthManager.getTokenId(Request.QueryString["code"]); 
          SesssionInstaUser.set(token); 
          //set login button to have option to log out 
          btnInstaAuth.Text = token.user.username + " leave instagtam oAuth"; 
          Response.Redirect(Request.Url.ToString().Split('?')[0]); 
        } 
        else 
         if (Request.QueryString["error"] != null) 
         { 
          btnInstaAuth.Text = Request.QueryString["error_description"]; 
         } 
       } 
      } 

對不起,如果我已經錯過一些東西,PHP捲曲到C#是在第一類實現。

更新(我沒有忘記的東西),斯塔用戶OBJ :)

namespace InstagramLogin.Code 
{ 
    public class UserLogged 
    { 
     public string id { get; set; } 
     public string username { get; set; } 
     public string full_name { get; set; } 
     public string profile_picture { get; set; } 
    } 

    public class AuthUserApiToken 
    {  
     public string access_token { get; set; } 
     public UserLogged user { get; set; } 
    } 
}