Web Request Failed

Mar 22, 2015 at 2:37 PM
Edited Mar 22, 2015 at 2:37 PM
Hi

Trying to get the library working, but it's failing no matter what I try to do, always with the same error:

"https://api.twitter.com/oauth/access_token web request failed"

At the moment I'm just trying to collect the access tokens:
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(Settings.GetString("TwitterConsumerKey"), Settings.GetString("TwitterConsumerSecret"));

var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(oContext.Request.Url.AbsoluteUri, applicationCredentials);
What am I missing?
Coordinator
Mar 23, 2015 at 10:55 AM
Edited Mar 23, 2015 at 10:56 AM
Hi,

Could you use the ExceptionHandler and give me more details.
var exceptionDetails = ExceptionHandler.GetLastException().ToString();
Cheers,
Linvi
Mar 23, 2015 at 4:16 PM
Edited Mar 23, 2015 at 4:27 PM
The ExceptionHandler doesn't provide any further information:
"'((object)(ExceptionHandler.GetLastException()))' is null"

When I look at "Exception.GetBaseException()" I get the following

[Tweetinvi.Logic.Exceptions.TwitterException]: {"https://api.twitter.com/oauth/access_token web request failed."}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    HResult: -2146233079
    InnerException: null
    Message: "https://api.twitter.com/oauth/access_token web request failed."
    Source: "Tweetinvi.WebLogic"
    StackTrace: "   at Tweetinvi.WebLogic.WebRequestExecutor.ExecuteWebRequest(HttpWebRequest webRequest)
   at Tweetinvi.WebLogic.TwitterRequester.ExecuteQueryWithTemporaryCredentials(String url, HttpMethod httpMethod, ITemporaryCredentials temporaryCredentials, IEnumerable`1 parameters)
   at Tweetinvi.Credentials.CredentialsCreator.GetCredentialsFromVerifierCode(String verifierCode, ITemporaryCredentials temporaryCredentials)
   at Tweetinvi.CredentialsCreator.GetCredentialsFromVerifierCode(String verifierCode, ITemporaryCredentials temporaryCredentials)
   at Tweetinvi.CredentialsCreator.GetCredentialsFromCallbackURL(String callbackURL, ITemporaryCredentials temporaryCredentials)
   at WhiteCobalt.CRM.Objects.SocialMediaAuth.CompleteAuth(HttpContext oContext, Page oPage) in C:\\dev\\WhiteCobalt.CRM\\DLL\\WhiteCobalt.CRM\\Objects\\SocialMediaAuth.cs:line 285"
    TargetSite: {System.String ExecuteWebRequest(System.Net.HttpWebRequest)}
Coordinator
Mar 23, 2015 at 5:18 PM
Edited Mar 23, 2015 at 5:21 PM
Hi,

Thank you for these information. For some reason Tweetinvi does not handle the WebException for you when you use the CredentialsCreator.
I have created a new Work Item and this will be fixed in the next release of Tweetinvi (0.9.6.0).
You can vote +1 on it if you want to be notified when a fixed is released.

In the meantime in order to help you could you execute the following code and tell me what are the exception details:
try
{
    var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(Settings.GetString("TwitterConsumerKey"), Settings.GetString("TwitterConsumerSecret"));
    var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(oContext.Request.Url.AbsoluteUri, applicationCredentials);
}
catch (TwitterException ex)
{
    ExceptionHandler.AddWebException(ex, ex.URL);

    // Here I need as much information as you can provide me!!!
    var exceptionDetails = ExceptionHandler.GetLastException().ToString();
}
// You will need to add the following namespace
using Tweetinvi.Logic.Exceptions;
Cheers,
Linvi
Coordinator
Mar 23, 2015 at 5:32 PM
A fix has already been released and the Exception will now be available in the ExceptionHandler.
This fix is only accessible from the Source Code version up until the release of Tweetinvi 0.9.6.0.

Please keep me up to date with the result of the previous post.

Regards,
Linvi
Mar 23, 2015 at 5:41 PM
The result of GetLastException() :
[Tweetinvi.Logic.Exceptions.TwitterException]: {"https://api.twitter.com/oauth/access_token web request failed."}
    CreationDate: {23/03/2015 16:41:00}
    StatusCode: -1
    TwitterDescription: null
    TwitterExceptionInfos: null
    URL: "https://api.twitter.com/oauth/access_token"
    WebException: {"https://api.twitter.com/oauth/access_token web request failed."}
Coordinator
Mar 23, 2015 at 5:49 PM
These information are not helping me.

Could you please send me the following :
var exceptionDetails = ExceptionHandler.GetLastException().ToString();
Linvi
Mar 23, 2015 at 6:43 PM
That line of code throws a null reference exception.

When i run it in the immediate window without the .ToString() i get the response i posted above.
Coordinator
Mar 23, 2015 at 6:46 PM
Thanks,

I will investigate and get back to you.
Sorry for this issue and thank you for reporting it.

Linvi
Coordinator
Mar 23, 2015 at 7:07 PM
Hi again,

Please send me the result of the following code.
try
{
    var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(Settings.GetString("TwitterConsumerKey"), Settings.GetString("TwitterConsumerSecret"));
    var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(oContext.Request.Url.AbsoluteUri, applicationCredentials);
}
catch (TwitterException ex)
{
    var detailsToSendToLinvi = string.Format("status code : {0}\r\n", ex.StatusCode);
    detailsToSendToLinvi += string.Format("description : {0}\r\n", ex.TwitterDescription);

    if (ex.TwitterExceptionInfos != null && ex.TwitterExceptionInfos.Count() > 0)
    {
        detailsToSendToLinvi += "--- DETAILS \r\n";

        foreach (var details in ex.TwitterExceptionInfos)
        {
            detailsToSendToLinvi += string.Format("{0} : {1}", details.Code, details.Message);
        }
    }

    // SEND ME THIS
    Console.WriteLine(detailsToSendToLinvi);
}
As a result of your request I will add a feature to add TwitterException to the ExceptionHandler and get the expected result from it.

Looking forward to hearing from you.
Linvi
Mar 23, 2015 at 10:36 PM
Edited Mar 23, 2015 at 10:37 PM
HI

That code fails to compile, with the following error:

Error 89 'System.Collections.Generic.IEnumerable<Tweetinvi.Core.Exceptions.ITwitterExceptionInfo>' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Tweetinvi.Core.Exceptions.ITwitterExceptionInfo>' could be found (are you missing a using directive or an assembly reference?) C:\dev\WhiteCobalt.CRM\DLL\WhiteCobalt.CRM\Objects\SocialMediaAuth.cs 320 94 WhiteCobalt.CRM

On "ex.TwitterExceptionInfos.Count()"

I'm running 0.9.5.2
Coordinator
Mar 23, 2015 at 10:40 PM
We will do it eventually!

You need to use linq:
using System.Linq;
Linvi
Mar 23, 2015 at 10:47 PM
status code : 401
description : Unauthorized - Authentication credentials were missing or incorrect.
--- DETAILS
0 : Invalid request token
Coordinator
Mar 23, 2015 at 11:35 PM
Alright,

Here you go, your credentials are incorrect.
Can you please check on apps.twitter.com.

You need to specify the Consumer Key (API Key) and the Consumer Secret (API Secret).
You also need to make sure they are valid.

If you continue to encounter this issue use the button Regenerate Consumer Key and Secret

Regards,
Linvi
Mar 24, 2015 at 12:02 AM
I have checked the credentials and they are correct. I've regenerated them just in case but the problem is still present.

If they weren't correct I don't think I would successfully get this screen?

Image
Coordinator
Mar 24, 2015 at 1:57 AM
Ok,

So if you are able to access to your app it implies that your credentials are correct.
Could you please have a look at the following code and verify that you enter the correct format of the CallbackURL.
The callback url is an URL that contains both and OAuthToken and an OAuthVerifier as the following example:

https://tweetinvi.codeplex.com/?oauth_token=aEXdqtdAZCSSlcYwiNcqhwZ93TO6Hh2Z&oauth_verifier=C0tAbhyTK74VnVHfgVzK1minL4adqde6e
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(consumerKey, consumerSecret);
var url = CredentialsCreator.GetAuthorizationURLForCallback(applicationCredentials, "https://tweetinvi.codeplex.com");
Console.WriteLine("Go on : {0}", url);
Console.WriteLine("When redirected to your website copy and paste the URL: ");

// Enter a value like: https://tweeetinvi.codeplex.com?oauth_token={tokenValue}&oauth_verifier={verifierValue}

var callbackURL = Console.ReadLine();

// Here we provide the entire URL where the user has been redirected
var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(callbackURL, applicationCredentials);
Console.WriteLine("Access Token = {0}", newCredentials.AccessToken);
Console.WriteLine("Access Token Secret = {0}", newCredentials.AccessTokenSecret);

return newCredentials;
Please check this as this code is working properly for me.
Cheers,
Linvi
Mar 24, 2015 at 12:15 PM
Hi

The callback URL comes from this code:
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(Settings.GetString("TwitterConsumerKey"), Settings.GetString("TwitterConsumerSecret"));
sURL = CredentialsCreator.GetAuthorizationURLForCallback(applicationCredentials, sCMSURL + "Social/Confirm_Auth.aspx?AuthID=" + this.PKValue.ToString());
(and the user is then redirected to the value in 'sURL')

The value in the second step of the process:
var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(oContext.Request.Url.AbsoluteUri, applicationCredentials);
"oContext.Request.Url.AbsoluteUri" contains the "oauth_token" and "oauth_verifier" as indicated.
Mar 24, 2015 at 12:37 PM
Is it something to do with me having an extra parameter in the querystring?
Coordinator
Mar 25, 2015 at 1:33 AM
I don't think so cause it is working properly on my machine.

Can you print of the oContext.Request.Url.AbsoluteUri as well as the applicationCredentials and verify that they are both correct.
Could you send me your credentials on my contact page so that I can test here and potentially see what is wrong with your credentials?

Cheers,
Linvi
Marked as answer by linvi on 4/18/2015 at 2:30 PM