0

i am trying to learn tweetinvi credential, so i have the following UI

enter image description here

and these 2 method, initAuthentication and startAuthentication

    private void initAuthentication()
    {            
            var appCredentials = new TwitterCredentials(consumerKey, consumerSecret);
            var authenticationContext = AuthFlow.InitAuthentication(appCredentials);
            Process.Start(authenticationContext.AuthorizationURL);

            //i am commenting the following code

            //var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(textBox1.Text, authenticationContext);
            //Auth.SetCredentials(userCredentials);

            //so the user have some time to copy the pinCode
            //paste them into available textbox
            // and continue executing startAuthentication() by clicking authenticate button.
    }

private void startAuthentication (string pinCode)
{
    //if we split like this, the authenticationContext is error, because it doesn't exist in current context

    var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pinCode, authenticationContext);
    Auth.SetCredentials(userCredentials);
}

if we join both part in one function, there's no time for user to copy paste the pin code into textbox.

however, if we split both part to different function, there's some time for user to copy paste the pin code into textbox, but it seems authenticationContext got error because it doesn't exist in current context

is there any way how to resolve this ?

Cignitor
  • 891
  • 3
  • 16
  • 36
  • 1
    you can only use var inside a local scope not in globar – Mostafiz Apr 23 '16 at 14:08
  • @MostafizurRahman agree, but how to bypass or make my code work like i expected ? – Cignitor Apr 23 '16 at 14:10
  • You want to make the dialog box modal then it won't return to the application before the authenticate button is pressed. – Hogan Apr 23 '16 at 14:20
  • Hello there, I am the developer of tweetinvi. I can tell that both answers are correct. There are many ways to do what you want but the simplest one is to store the variable in a private property. – Linvi Apr 25 '16 at 00:44

2 Answers2

3

I'm not sure how this API is supposed to work but the scope of a variable created within a method only exists within that method.

You have two options:

  1. Make a class variable of whatever type is returned by AuthFlow.InitAuthentication(appCredentials);, set it in your first method and then you can access it from the second method.

  2. Add parameter of whatever the return type of AuthFlow.InitAuthentication(appCredentials); is to your second method and then pass in the context when you are calling the second method.

Edit

Just thought about this a bit more, context is usually something you need to pass around quite a bit so option 1 might be better.

Edit 2

I looked it up and InitAuthentication returns IAuthenticationContext. So make a class variable outside of your methods like this

IAuthenticationContext _authContext;

Then in method one

_authContext = AuthFlow.InitAuthentication(appCredentials);

Then in method two

var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pinCode, _authContext);

Also to answer the question about var... var is basically shorthand to create a variable without having to type out the type each time. However the one caveat to var is that the variable it's being used with has to be assigned some value otherwise var has no way of inferring the underlying type and you will get a compiler error.

For example

var myVariable1;

or

var myVariable1 = null;

Will throw a compile time error because the compiler has no way of inferring what the Type for myVariable1 should be.

The correct syntax is

var myVariable1 = 4;

Or

var myVariable1 = "hello world";

Or

var myVariable1 = SomeMethodThatReturnsSomething();
Dmitry K.
  • 972
  • 6
  • 16
0

Create class field instead of local variables

class ClassName
{
    AuthenticationContext authenticationContext;

    private void initAuth()
    {
        // set authenticationContext
        authenticationContext = AuthFlow.InitAuthentication(appCredentials);
    }

    private void startAuth(string pin)
    {
        // use authenticationContext
    }
}