TwitterApiError in Windows Phone 7

Nov 4, 2012 at 5:35 AM

Hi!

I'm using the example you have in this page for login in into my app using twitter, but the problem appears here:

        private void AuthenticateButton_Click(object sender, RoutedEventArgs e)
        {
            pinAuth.CompleteAuthorize(
                PinTextBox.Text,
                completeResp => Dispatcher.BeginInvoke(() =>
                {
                    switch (completeResp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            var cred = pinAuth.Credentials as IsolatedStorageCredentials;
                            cred.Save();
                            IsolatedStorageSettings.ApplicationSettings.Add("idTwitter", cred.UserId);
                            IsolatedStorageSettings.ApplicationSettings.Save();
                            var url = string.Format("/Pages/TwitterInfoPage.xaml?id={0}",cred.UserId);
                            NavigationService.Navigate(new Uri(url, UriKind.Relative));
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show(
                                completeResp.Error.ToString(),
                                completeResp.Message,
                                MessageBoxButton.OK);
                            break;
                    }
                }));
        }

Sometimes "completeResp.Status" shows TwitterApiError as a result, but in some other moments it appears as Success, and i dont know what to do make it debug without problems.

Cheers,

EAGLE

Coordinator
Nov 4, 2012 at 7:20 AM

Hi Eagle,

When there's an error, typically something has gone wrong in communicating with Twitter. Look at the Exception, print it out and post it here so I can give you better advice. When looking at the exception, be sure to get inner exception info, if available, because sometimes that contains the answer to what the problem is.  Also, if you get the Response from TwitterQueryException (A LINQ to Twitter exception Type) because that contains the error message from Twitter. HTTP 401 errors are very common, so check out the LINQ to Twitter FAQ (http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20FAQ&referringTitle=Documentation) for more guidance. Finally, remember that Fiddler (http://www.fiddler2.com/fiddler2/) is your friend.

Joe

Nov 4, 2012 at 8:37 PM

Hi Joe!

Well, i'm really noob to Windows Phone SDK and pretty much i don't know how to copy the message it appears, but debugging it i have this info.

_message = "The remote server returned an error: NotFound."

StackTrace = "      at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)   at System.Delegate.DynamicInvokeOne(Object[] args)   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)   at System.Delegate.DynamicInvoke(Object[] args)   at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)   at System.Delegate.DynamicInvokeOne(Object[] args)   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)   at System.Delegate.DynamicInvoke(Object[] args)   at System.Windows.Threading.DispatcherOperation.Invoke()   at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)   at System.Windows.Threading.Dispatcher.OnInvoke(Object context)   at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)   at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)   at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)"

I hope this could be useful to understand it, because i don't have a clue what to do with this.

Thanks,

EAGLE

Coordinator
Nov 4, 2012 at 9:43 PM

The NotFound is what Twitter usually returns when the request tries to communicate with an endpoint that isn't there. It could also mean that a parameter is missing. In the case of completing the authorization process, it might even mean that credentials aren't complete. These are guesses because I don't have enough information here that tells me definitively what the problem is. Here are some things that you might look at:

1. Did you go through the entire authorization process before calling CompleteAuthorize?

2. Is there a TwitterQueryException returned that has a Response property?

3. Is there an InnerException that contains more information?

4. What does Fiddler show as Twitter's response?

5. I noticed you're building on WP7 - did you set the clock on your emulator to the correct time? They must match Twitter's server time, which you can get from the Fiddler response or look at the Headers collection on the TwitterContext instance after the query response.

6. The NotFound might not be a 100% accurate response and I'm saying this because the errors are occurring during authentication. Typically, authentication errors show up as 401 errors. If you discover that this is really a 401 authentication problem, check out my FAQ:

http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20FAQ&referringTitle=Documentation

Important: Make sure you sanitize any credential data before posting.

BTW, this might seem like a tough process to go through, but the debugging skills you learn from solving this problem will help later on when debugging other Windows Phone issues involving network communications with a REST API.

Joe