2013-10-02 49 views
0

所以我基本上嘗試與Foursquare的venue API東西,但得到奇怪的錯誤,每當我發送一個API請求。Xamarin.Auth - 「一個或多個錯誤發生」與Foursquare場地API

首先我使用下面的代碼進行身份驗證;

var foursquareClient = new FoursquareClient(this); 
     if(!foursquareClient.IsAuthenticated) 
      foursquareClient.Authenticate();` 

驗證碼;

public void Authenticate() 
    { 
     Log.Verbose(Logging.AppTag, "FourSquareClient:Authenticate()"); 

     if (this.IsAuthenticated) 
     { 
      Log.Debug(Logging.AppTag, "FourSquareClient is already authenticated! "); 
      return; 
     } 

     this._authenticator = new OAuth2Authenticator(
      clientId: ClientId, 
      scope: "", 
      authorizeUrl: new Uri("https://foursquare.com/oauth2/authenticate"), 
      redirectUrl: new Uri("http://www.int6.org")) 
     { 
      AllowCancel = false 
     }; 

     this._authenticator.Completed += (sender, eventArgs) => 
     { 
      if (eventArgs.IsAuthenticated) // auth. completed. 
      { 
       this.StoreAccount(eventArgs); 

       var builder = new AlertDialog.Builder(this.OwnerContext); 
       builder.SetMessage("auth all good!"); 
       builder.SetPositiveButton("Ok", (o, e) => { }); 
       builder.Create().Show(); 
       return; 
      } 
      else // The user cancelled. 
      { 
       var builder = new AlertDialog.Builder(this.OwnerContext); 
       builder.SetMessage("User canncelled!"); 
       builder.SetPositiveButton("Ok", (o, e) => { }); 
       builder.Create().Show(); 
       return; 
      } 
     }; 

     // show the authenticator UI and start auth. 
     var intent = this._authenticator.GetUI(this.OwnerContext); 
     this.OwnerContext.StartActivity(intent); 
    } 

因此,如果用戶被驗證一次,它會將該帳戶存儲在設備上。

public FoursquareClient(Context context) 
{ 
    Log.Verbose(Logging.AppTag, "Init foursquare client.."); 

    this.OwnerContext = context; // make sure we set the owner context before any. 
    this.RetrieveAccount(); // try to retrieve any existing accounts. 
} 

每當他再次打開該應用程序,該帳戶將被加載回;

private void RetrieveAccount() 
{ 
    if (this.IsAuthenticated) 
    { 
     Log.Debug(Logging.AppTag, "FourSquareClient is already authenticated! "); 
     return; 
    } 

    var accounts = AccountStore.Create(this.OwnerContext).FindAccountsForService("Foursquare"); 
    var enumerable = accounts as IList<Account> ?? accounts.ToList(); 

    if (enumerable.Any()) 
    { 
     Log.Info(Logging.AppTag, "Foursquareclient found account data."); 
     this.IsAuthenticated = true; 
     this.Account = enumerable.First(); 
    } 
    else 
    { 
     Log.Info(Logging.AppTag, "Foursquareclient no account data found!"); 
     this.IsAuthenticated = false; 
     this.Account = null; 
    } 
} 

所以我想我都認爲auth的東西不錯,但不知何故無法提出請求;

public string MakeRequest() 
{ 
    var @params = new Dictionary<string, string> 
     { 
      {"v", "20120321"}, 
      {"ll", "44.3,37.2"} 
     }; 
    var request = new OAuth2Request("GET", new Uri("https://api.foursquare.com/v2/venues/explore"), @params, 
            this.Account); 

    request.GetResponseAsync().ContinueWith(t => 
     { 
      if (t.IsFaulted) 
       Console.WriteLine(t.Exception.Flatten()); 
      else 
      { 
       string json = t.Result.GetResponseText(); 
       Console.WriteLine(json); 
      } 
     }); 

    return string.Empty; 
} 

請求碼返回; 一個或多個錯誤發生

10-02 14:54:31.403 I/mono-stdout(8641): System.AggregateException: One or more errors occured ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. System.AggregateException: One or more errors occured ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. 10-02 14:54:31.413 I/mono-stdout(8641): at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0 at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0 at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0 10-02 14:54:31.413 I/mono-stdout(8641): at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0 10-02 14:54:31.413 I/mono-stdout(8641): --- End of inner exception stack trace --- --- End of inner exception stack trace --- --> (Inner exception 0) System.Net.WebException: The remote server returned an error: (400) Bad Request. 10-02 14:54:31.423 I/mono-stdout(8641): --> (Inner exception 0) System.Net.WebException: The remote server returned an error: (400) Bad Request. 10-02 14:54:31.423 I/mono-stdout(8641): at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0 at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0 10-02 14:54:31.423 I/mono-stdout(8641): at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0 at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0

對我失去了我的任何想法?

回答

0

我已經通過使用RestSharp解決了問題;

 // https://developer.foursquare.com/docs/venues/explore 
    public string GetVenues() 
    { 
     var @params = new Dictionary<string, string> 
      { 
       {"v", "20120321"}, 
       {"ll", "44.3,37.2"} 
      }; 

     var response = this.Request("venues/explore", @params); 

     return response; 
    } 

    private string Request(string endpoint, Dictionary<string, string> @params = null, HttpMethod httpMethod = HttpMethod.GET, bool userless = false) 
    { 
     var client = new RestClient("https://api.foursquare.com/v2/");    
     var request = new RestRequest(endpoint, Method.GET); 

     if (!userless) 
     { 
      // About userless requests - https://developer.foursquare.com/overview/auth.html - Some of our endpoints that don’t pertain to 
      // specific user information, such as venues search are enabled for userless access (meaning you don’t need to have a user auth your 
      // app for access). To make a userless request, specify your consumer key's Client ID and Secret instead of an auth token in the request URL. 

      request.AddParameter("oauth_token", this.Account.Properties["access_token"]); 
     } 

     if (@params != null) 
     { 
      foreach (var param in @params) 
      { 
       request.AddParameter(param.Key, param.Value); 
      } 
     } 

     var response = client.Execute(request); 
     var content = response.Content; // raw content as string 

     return content; 
    } 
0

我只是用你的第一個代碼,但是手工添加了「組oauth_token」參數,它工作得很好:)

我的代碼看起來是這樣的:

var @params = new Dictionary<string, string> 
     { 
      {"oauth_token", this.Account.Properties["access_token"]}, 
      {"v", "20120321"}, 
      {"ll", "44.3,37.2"} 
     }; 
相關問題