1

我正在使用以下内容来阅读 Twitter json。它适用于一个 uri 而不是另一个。uri 适用于 Twitter API 控制台,但不适用于 Xamarin.Social。我在 Twitter 应用上拥有读写权限,所以我看不出哪里出错了。

https://api.twitter.com/1.1/account/settings.json   <-- works
https://api.twitter.com/1.1/users/show.json?screen_name=AUserName   <-- fails (see error below)


request.GetResponseAsync ().ContinueWith (response => {

            if (response.IsFaulted)
            {
                Console.WriteLine (response.Exception.Flatten ()); 
            }

            var json = response.Result.GetResponseText ();


System.AggregateException: One or more errors occured ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x0030c] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1606 
   at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00141] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1423 
   --- End of inner exception stack trace ---
   --> (Inner exception 0) System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x0030c] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1606 
   at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00141] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1423 

[快速谷歌搜索给出了这个但不确定它是否相关:https://dev.twitter.com/discussions/15206]

// UPDATE ***********

这是额外的信息帮助还是您需要更多详细信息?如果是这样,那么需要哪些详细信息?

public Account Account
    {
        get
        {
            var task = Service.GetAccountsAsync ()
                .ContinueWith (accounts => 
            {
                return accounts.Result.ToList ().FirstOrDefault ();
            });

            return task.Result;
        }
        set
        {
            AccountStore.Create ().Save (value, SocialPlatform.ToString ());
        }
    }


// later on
// when endpoint = "https://api.twitter.com/1.1/account/settings.json" <-- works, json returned
// when endpoint = "https://api.twitter.com/1.1/users/show.json?screen_name=XXXX" <-- IsFaulted with above error, 

var request = Service.CreateRequest ("GET", endpoint, Account);

        request.GetResponseAsync ().ContinueWith (response => {

            if (response.IsFaulted)
            {
                Console.WriteLine (response.Exception.Flatten ());
                return;
            }

            var json = response.Result.GetResponseText ();
            Console.WriteLine (json);
        });
4

1 回答 1

0

拨打此电话时,您似乎未获得授权。

来自 Xamarin.Social 文档。

Xamarin.Social 使用 Xamarin.Auth 库来获取和存储帐户对象。

每个服务公开一个 GetAuthenticateUI 方法,该方法返回一个 Xamarin.Auth.Authenticator 对象,您可以使用该对象对用户进行身份验证。这样做会自动存储经过身份验证的帐户,以便以后使用。

它在 Twitter API 控制台中工作的原因是您在拨打电话之前已经在那里授权。

如果您已经在您的应用中授权,请发布您用于授权的代码。

于 2013-09-12T03:44:22.413 回答