-
Notifications
You must be signed in to change notification settings - Fork 14
/
TwitterController.cs
118 lines (100 loc) · 4.42 KB
/
TwitterController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using FurlandGraph.Models;
using FurlandGraph.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.Net;
using System.Net.Http.Headers;
using Tweetinvi;
using Tweetinvi.Auth;
using Tweetinvi.Parameters;
namespace FurlandGraph.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TwitterController : ControllerBase
{
private static readonly IAuthenticationRequestStore _myAuthRequestStore = new LocalAuthenticationRequestStore();
public TwitterController(FurlandContext context, IOptions<TwitterConfiguration> twitterConfiguration, UserService userService)
{
Context = context;
TwitterConfiguration = twitterConfiguration;
UserService = userService;
}
public FurlandContext Context { get; }
public IOptions<TwitterConfiguration> TwitterConfiguration { get; }
public UserService UserService { get; }
[HttpGet]
[Route("{id}/picture")]
public async Task<ActionResult> GetProfilePicture(long id)
{
var picture = await Context.ProfilePictures.FindAsync(id);
if(picture == null)
{
return NotFound();
}
Response.Headers["cache-control"] = "public, max-age=604800";
return File(picture.Data, "image/png");
}
[HttpGet]
[Route("redirect")]
public async Task<object> GetRedirectUrl()
{
var twitterConfig = TwitterConfiguration.Value;
var appClient = new TwitterClient(twitterConfig.ConsumerKey, twitterConfig.ConsumerSecret);
var authenticationRequestId = Guid.NewGuid().ToString();
var redirectPath = "https://" + Request.Host.Value + "/validate/twitter";
// var redirectPath = "https://graph.bunnypa.ws/validate/twitter";
// Add the user identifier as a query parameters that will be received by `ValidateTwitterAuth`
var redirectURL = _myAuthRequestStore.AppendAuthenticationRequestIdToCallbackUrl(redirectPath, authenticationRequestId);
// Initialize the authentication process
var authenticationRequestToken = await appClient.Auth.RequestAuthenticationUrlAsync(redirectURL);
// Store the token information in the store
await _myAuthRequestStore.AddAuthenticationTokenAsync(authenticationRequestId, authenticationRequestToken);
// Redirect the user to Twitter
return new
{
AuthorizationURL = authenticationRequestToken.AuthorizationURL
};
}
[HttpPost]
[Route("validate")]
public async Task<object> Validate()
{
var twitterConfig = TwitterConfiguration.Value;
var appClient = new TwitterClient(twitterConfig.ConsumerKey, twitterConfig.ConsumerSecret);
var requestParameters = await RequestCredentialsParameters.FromCallbackUrlAsync(Request.QueryString.Value, _myAuthRequestStore);
var userCreds = await appClient.Auth.RequestCredentialsAsync(requestParameters);
var userClient = new TwitterClient(userCreds);
var user = await userClient.Users.GetAuthenticatedUserAsync();
var tokenRow = await Context.TwitterTokens.Where(t => t.Id == user.Id).FirstOrDefaultAsync();
if (tokenRow == null)
{
Context.TwitterTokens.Add(new TwitterToken()
{
Id = user.Id,
AccessSecret = userCreds.AccessTokenSecret,
AccessToken = userCreds.AccessToken,
BearerToken = userCreds.BearerToken,
NextFriendsRequest = DateTime.UtcNow,
});
}
else
{
tokenRow.AccessSecret = userCreds.AccessTokenSecret;
tokenRow.AccessToken = userCreds.AccessToken;
tokenRow.BearerToken = userCreds.BearerToken;
}
await UserService.CollectUser(Context, user);
await Context.SaveChangesAsync();
// HttpContext.Session.SetString("userId", user.IdStr);
return new
{
Id = user.Id.ToString(),
ScreenName = user.ScreenName,
Name = user.Name,
};
}
}
}