usingSystem;usingDevDefined.OAuth.Consumer;usingDevDefined.OAuth.Framework;usingNewtonsoft.Json;namespaceoauth1robot{publicclassProfile{publicintid{get;set;}publicStringfirst_name{get;set;}publicStringlast_name{get;set;}}classMainClass{publicstaticvoidMain(string[]args){stringrequestTokenUrl="https://api.projectplace.com/initiate";stringauthorizationUrl="https://api.projectplace.com/authorize";stringtokenUrl="https://api.projectplace.com/token";stringapiEndpoint="https://api.projectplace.com";stringconsumerKey="APPLICATION_KEY_GOES_HERE";stringconsumerSecret="APPLICATION_SECRET_GOES_HERE";ITokenaccessToken=null;// 1. If you already have an access token - uncomment this section and enter it here.//IToken accessToken = new TokenBase();//accessToken.Token = "ACCESS_TOKEN_KEY_GOES_HERE";//accessToken.TokenSecret = "ACCESS_TOKEN_SECRET_GOES_HERE";// 2. Create the consumer contextOAuthConsumerContextconsumerContext=newOAuthConsumerContext{ConsumerKey=consumerKey,ConsumerSecret=consumerSecret,SignatureMethod=SignatureMethod.HmacSha1,UseHeaderForOAuthParameters=true,};// 3. Start sessionOAuthSessionsession=newOAuthSession(consumerContext,requestTokenUrl,authorizationUrl,tokenUrl);// 4. If you do not have an access token, you will first have to authorize// access. In this part we formulate a URI which you must open in a web-broser.// Once you have completed the log-in and accepted access for the application// You will be redirected to whatever page is in the applications callback// Simply check the URL and look for the oauth_verifer parameter, and copy thatif(accessToken==null){ITokenrequestToken=session.GetRequestToken();stringauthorizationLink=session.GetUserAuthorizationUrlForToken(requestToken);Console.WriteLine("Authorize this application by going to {0}",authorizationLink);Console.WriteLine("Then enter the oauth_verifier here:");stringverificationCode=Console.ReadLine();accessToken=session.ExchangeRequestTokenForAccessToken(requestToken,verificationCode);Console.WriteLine("Here is your new access token: {0} with secret: {1}\n",accessToken.Token,accessToken.TokenSecret);}// 5. We have an access token - assign it to the session session.AccessToken=accessToken;// 6. Lets ask for a protected resource, such as your own profilestringresponseText=session.Request().Get().ForUrl(apiEndpoint+"/1/user/me/profile").ToString();Profileprofile=JsonConvert.DeserializeObject<Profile>(responseText);Console.WriteLine("Successfully fetched profile for {0} {1}",profile.first_name,profile.last_name);}}}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.