所以我基本上是在尝试使用 Foursquare 的场地 API,但每当我发送 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: 发生一个或多个错误---> System.Net.WebException:远程服务器返回错误:(400)错误请求。 System.AggregateException:发生一个或多个错误---> System.Net.WebException:远程服务器返回错误:(400)错误请求。10-02 14:54:31.413 I/mono-stdout(8641):在 System.Net.HttpWebRequest.CheckFinalStatus(System.Net.WebAsyncResult 结果)[0x00000] 在:0 在 System.Net.HttpWebRequest.CheckFinalStatus(系统。 Net.WebAsyncResult 结果)[0x00000] in :0 在 System.Net.HttpWebRequest.SetResponseData(System.Net.WebConnectionData 数据)[0x00000] in :0 10-02 14:54:31.413 I/mono-stdout(8641):在 System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0 10-02 14:54:31.413 I/mono-stdout(8641): --- 内部异常堆栈跟踪结束 -- - --- End of internal 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):->(内部异常 0)System.Net.WebException:远程服务器返回错误:(400) 错误请求。10-02 14:54:31.423 I/mono-stdout(8641):在 System.Net.HttpWebRequest.CheckFinalStatus(System.Net.WebAsyncResult 结果)[0x00000] 在:0 在 System.Net.HttpWebRequest.CheckFinalStatus(系统。 Net.WebAsyncResult 结果)[0x00000] 在:0 10-02 14:54:31.423 I/mono-stdout(8641):在 System.Net.HttpWebRequest.SetResponseData(System.Net.WebConnectionData 数据)[0x00000] 在:0在 System.Net.HttpWebRequest.SetResponseData(System.Net.WebConnectionData 数据)[0x00000] 中:0
关于我所缺少的任何想法?