Linq to twitter wp8 implementation - c#

I’m trying to get Linq to twitter to work on a wp8 App, i’ve looked throughout the available documentation and was not able to figure this out, when trying the demos (which the author says works for wp8) i get errors.
Im using the ApplicationOnlyAuthorizer, my intention is just to be able to read the public tweets (not be able to log in and send tweets).
MainPage.cs
public MainPage()
{
this.InitializeComponent();
UserTweetsWidget = new UserTweetsViewModel("xxxxxxxx", 20);
this.DataContext = this;
}
TweetModel.cs
public class TweetModel
{
public string ScreenName { get; set; }
public string UserName { get; set; }
public string Image { get; set; }
public string Text { get; set; }
public string PublicationDate { get; set; }
}
UserTweetsViewModel.cs
public class UserTweetsViewModel
{
public string Label { get; set; }
public ObservableCollection<TweetModel> Tweets { get; set; }
private const string consumerKey = “xxxxxxx”;
private const string consumerSecret = “xxxxxx”;
private const string twitterAccessToken = “xxxxxxxxxxxxxxxxx”;
private const string twitterAccessTokenSecret = “xxxxxxxxxx”;
public UserTweetsViewModel(string userName, int count)
{
String _n= userName;
int _c= count;
InitializeAsync(_n, _c);
}
private async Task InitializeAsync(string userName, int count)
{
this.Label = string.Format("Tweets by #{0}", userName);
Tweets = await GetTwitterUserTimeLine(userName, count);
}
private async Task<ObservableCollection<TweetModel>> GetTwitterUserTimeLine(string userName, int count)
{
ObservableCollection<TweetModel> result = new ObservableCollection<TweetModel>();
var auth = new ApplicationOnlyAuthorizer
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = consumerKey,
ConsumerSecret = consumerSecret,
OAuthToken= twitterAccessToken,
OAuthTokenSecret= twitterAccessTokenSecret
}
};
await auth.AuthorizeAsync();
TwitterContext twitterCtx = new TwitterContext(auth);
var tweets = twitterCtx.Status.Where(tweet => tweet.ScreenName == userName && tweet.Type == StatusType.Home).Take(count).ToList();
foreach (var item in tweets)
{
TweetModel tweet = new TweetModel()
{
Text = item.Text,
ScreenName = item.User.Name,
UserName = "#" + item.ScreenName,
PublicationDate = Convert.ToString(item.CreatedAt),
Image = item.User.ProfileImageUrl
};
result.Add(tweet);
}
return result;
}
}
Thanks,
Bob

The problem, i think, is you aren't awaiting the AuthorizeAsync method.
First, you need to convert your GetTwitterUserTimeLine into an async method:
private async Task<ObservableCollection<TweetModel>> GetTwitterUserTimeLine(string userName, int count)
Then, you can call auth.AuthorizeAsync, awaiting the call:
await auth.AuthorizeAsync();
This way, the method waits until the authorization is completed, before continue execution.
UPDATE:
To call this method from your viewmodel, You can create a new method in the ViewModel, called InitializeAsync:
private async Task InitializeAsync()
In that method, call the GetTwitterUserTimeLine this way:
Tweets = await GetTwitterUserTimeLine(username, count);
Finally, you need to call the InitializeAsync Method from your viewmodel constructor, and it's going to work.
Hope this helps.

Related

Create a schedule for a runbook in automation with C#

So, I have been struggling with this for a while now, but I am getting a bit further each day. But now I am really stuck.
What I am trying to do:
I have an automation account with a runbook (PowerShell)
I want to create a schedule
Then I want to connect that schedule to the runbook so the runbook is executed at a specific date and time.
I want to achieve this through C# with the Management API of Azure. I found a few webpages that should give me all the information I needed, but it doesn't work.
The first one is this: https://learn.microsoft.com/en-us/rest/api/automation/schedule/create-or-update?tabs=HTTP
I get this to work and I can see the schedule after I ran the code.
The second one is this: https://learn.microsoft.com/en-us/rest/api/automation/job-schedule/create?tabs=HTTP
And here I get stuck. I get a 404 error when I execute the request and I have no idea what I am doing wrong.
The code below is far from perfect, but it's a POC and not the actual application. I want to make it work before I refactor.
This class has all the logic for the schedule:
public class ScheduleLogic
{
const string subscriptionId = "######";
const string resourceGroupName = "######";
const string automationAccountName = "######";
const string tenantId = "######";
const string clientId = "######";
const string clientSecret = "######";
const string resourceId = "https://management.azure.com/";
public string accessToken { get; set; }
// THIS ONE DOESN'T WORK!
public async Task ConnectScheduleToRunbook(RunbookSchedule runbookSchedule)
{
StringContent body = new(JsonSerializer.Serialize(runbookSchedule));
body.Headers.ContentType = new MediaTypeHeaderValue("application/json");
string url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/jobSchedules/{runbookSchedule.Properties.Schedule.JobScheduleId}?api-version=2019-06-01";
await ExecuteRequest(RequestType.Put, url, body);
}
public async Task CreateSchedule(Schedule schedule)
{
if (string.IsNullOrEmpty(schedule.JobScheduleId))
throw new Exception("Job schedule ID is empty");
StringContent body = new(JsonSerializer.Serialize(schedule));
body.Headers.ContentType = new MediaTypeHeaderValue("application/json");
string url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/schedules/{schedule.JobScheduleId}?api-version=2019-06-01";
await ExecuteRequest(RequestType.Put, url, body);
}
public async Task DeleteSchedule(string scheduleName)
{
string url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/schedules/{scheduleName}?api-version=2019-06-01";
await ExecuteRequest(RequestType.Delete, url, null);
}
private async Task ExecuteRequest(RequestType requestType, string url, StringContent? body)
{
HttpResponseMessage response;
using (HttpClient client = new())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {await GetAccessToken()}");
switch (requestType)
{
case RequestType.Put:
if (body == null)
throw new Exception("Body cannot be empty");
response = await client.PutAsync(url, body);
break;
case RequestType.Delete:
response = await client.DeleteAsync(url);
break;
default:
throw new Exception("Unknown request type");
}
if (!response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
throw new Exception(content);
}
}
}
private async Task<string> GetAccessToken()
{
if (!string.IsNullOrEmpty(accessToken))
return accessToken;
AuthenticationContext authContext = new("https://login.microsoftonline.com/" + tenantId);
ClientCredential clientCreds = new(clientId, clientSecret);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resourceId, clientCreds);
accessToken = authResult.AccessToken;
return accessToken;
}
}
Models:
public class RunbookSchedule
{
public RunbookScheduleProperties Properties { get; set; }
}
public class RunbookScheduleProperties
{
public Schedule Schedule { get; set; }
public Runbook Runbook { get; set; }
public object Parameters { get; set; }
}
public class Runbook
{
public string Name { get; set; }
}
public class Schedule
{
public string Name { get; set; }
[JsonIgnore]
public string JobScheduleId { get; set; }
[JsonPropertyName("properties")]
public ScheduleProperties ScheduleProperties { get; set; }
}
public class ScheduleProperties
{
public string Description { get; set; }
public DateTime StartTime { get; set; }
public DateTime ExpiryTime { get; set; }
public string Frequency { get; set; }
public object AdvancedSchedule => new { };
}
And the unit tests I use to test the logic:
public class ScheduleTests
{
private readonly string jobId = "d52004cc-b7ec-4b9b-99c1-3922492f6e1b-1";
private readonly string runbookName = "RunFunctionApp";
private readonly ScheduleLogic scheduleService;
public ScheduleTests()
{
scheduleService = new();
}
[Fact]
public async Task Should_CreateASchedule()
{
Schedule newScheduleSettings = new()
{
Name = jobId,
JobScheduleId = jobId,
ScheduleProperties = new()
{
Description = "Here is another example",
StartTime = new DateTime(2024, 03, 27, 9, 0, 0, 0),
ExpiryTime = new DateTime(2024, 03, 27, 10, 0, 0, 0),
Frequency = "OneTime"
}
};
await scheduleService.CreateSchedule(newScheduleSettings);
}
[Fact]
public async Task Should_DeleteSchedule()
{
await scheduleService.DeleteSchedule(jobId);
}
[Fact]
public async Task Should_ConnectScheduleAndRunbook()
{
await scheduleService.ConnectScheduleToRunbook(new RunbookSchedule
{
Properties = new()
{
Schedule = new() { Name = jobId, JobScheduleId = jobId },
Runbook = new() { Name = runbookName },
Parameters = new { id = 12 }
}
});
}
}
Somehow I think I am doing something stupid and the fix/answer is really simple.
I tried to mess around with the URL because I think there is a problem with that, not sure why... Just a feeling.
Google did show me some results, but those were for schedules of other services. Runbooks and automation don't seem to be very popular.
ChatGPT comes with older, obsolete solutions. Visual Studio gives a lot of green and even red lines when I try those suggestions.

Getting custom object of a Json with Async function

I'm trying to use the HUE API, I was able to get the JSON with the list of the bulb but when i'm trying to make a custom object from it, its not working. No error and no exception, nothing is happening. Here's my attempt :
LightsPage.xaml.cs
public partial class LightsPage : Page
{
public LightsPage()
{
InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var listLights = Light.GetLightAsync();
Console.WriteLine();
}
}
Light.cs
public class Light
{
private string name { get; set; }
private string productname { get; set; }
private string uniqueid { get; set; }
private StateLight state { get; set; }
public Light()
{
}
public Light(string Name, string Productname, string Uniqueid, StateLight State)
{
name = Name;
productname = Productname;
uniqueid = Uniqueid;
state = State;
}
static async public Task<List<Light>> GetLightAsync()
{
List<Light> lights = new List<Light>();
Uri baseUri = new Uri($"http://{ConfigurationManager.AppSettings["IpBridge"]}");
Uri apiUri = new Uri(baseUri, $"/api/{ConfigurationManager.AppSettings["userName"]}/lights");
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(apiUri);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
dynamic jsonResult = JsonConvert.DeserializeObject(responseBody);
foreach(var item in jsonResult)
{
Light light = new Light
{
name = (string)item["name"],
productname = (string)item["productname"],
uniqueid = (string)item["uniqueid"],
state = new StateLight
{
on = (bool)item["state"]["on"],
bri = (int)item["state"]["bri"]
}
};
lights.Add(light);
}
return lights;
}
}
The JSON looks like this :
{"1":{"state":{"on":true,"bri":254,"alert":"select","mode":"homeautomation","reachable":true},
"swupdate":{"state":"noupdates","lastinstall":"2020-02-22T14:21:56"},
"type":"Dimmable light",
"name":"Salon blanc",
"modelid":"LWA001",
"manufacturername":"Signify Netherlands B.V.",
"productname":"Hue white lamp",
"capabilities":{"certified":true,"control":{"mindimlevel":5000,"maxlumen":800},
"streaming":{"renderer":false,"proxy":false}}, etc....
There is multiple bulb but you get the point.
In debug, when i'm reaching the construction of the objects Light in the loop, it stops there.
I'm fairly new to Task and Async so maybe it's coming from here but I don't know what to do...
Thx for your answers :)

WPF application authentication with Google

I have found many different solutions with OAuth and either with some libraries or with pure requests (https://github.com/googlesamples/oauth-apps-for-windows).
However, none of the solutions looks like the one that I really need. Currently, my application uses its own database for users to log in using WCF service request (with username and password). However, all users have their domain e-mail created with Google accounts, so I want to add another button "Sign In with Google", which will just make sure that user can also login with his Google e-mail-password pair. I don't need a returned token for the further use etc.
What is the simplest way to achieve this functionality in WPF/C# desktop application?
Here is a self-sufficient, 3rd party-free, WPF sample that does Google auth (it could be easily converted to winforms as well).
If you run it you won't be logged and the app will show you a button. If you click that button, an embedded webbrowser control will run though the Google auth.
Once you are authenticated, the app will just display your name as returned by Google.
Note it's based on official Google's sample here: https://github.com/googlesamples/oauth-apps-for-windows but it uses an embedded browser instead of spawning an external one (and few other differences).
XAML code:
<Window x:Class="GoogleAuth.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="750" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="btv" />
</Window.Resources>
<Grid>
<DockPanel Visibility="{Binding State.IsSigned, Converter={StaticResource btv}}">
<Label>You are signed as:</Label>
<Label Content="{Binding State.Token.Name}" />
</DockPanel>
<Grid Visibility="{Binding State.IsNotSigned, Converter={StaticResource btv}}">
<Grid.RowDefinitions>
<RowDefinition Height="23" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Click="Button_Click">Click to Sign In</Button>
<WebBrowser Grid.Row="1" x:Name="Wb" Height="Auto" />
</Grid>
</Grid>
</Window>
C# code:
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace GoogleAuth
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
State = new OAuthState();
DataContext = this;
}
public OAuthState State { get; }
private void Button_Click(object sender, RoutedEventArgs e)
{
var thread = new Thread(HandleRedirect);
thread.Start();
}
private async void HandleRedirect()
{
State.Token = null;
// for example, let's pretend I want also want to have access to WebAlbums
var scopes = new string[] { "https://picasaweb.google.com/data/" };
var request = OAuthRequest.BuildLoopbackRequest(scopes);
var listener = new HttpListener();
listener.Prefixes.Add(request.RedirectUri);
listener.Start();
// note: add a reference to System.Windows.Presentation and a 'using System.Windows.Threading' for this to compile
await Dispatcher.BeginInvoke(() =>
{
Wb.Navigate(request.AuthorizationRequestUri);
});
// here, we'll wait for redirection from our hosted webbrowser
var context = await listener.GetContextAsync();
// browser has navigated to our small http servern answer anything here
string html = string.Format("<html><body></body></html>");
var buffer = Encoding.UTF8.GetBytes(html);
context.Response.ContentLength64 = buffer.Length;
var stream = context.Response.OutputStream;
var responseTask = stream.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
{
stream.Close();
listener.Stop();
});
string error = context.Request.QueryString["error"];
if (error != null)
return;
string state = context.Request.QueryString["state"];
if (state != request.State)
return;
string code = context.Request.QueryString["code"];
State.Token = request.ExchangeCodeForAccessToken(code);
}
}
// state model
public class OAuthState : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private OAuthToken _token;
public OAuthToken Token
{
get => _token;
set
{
if (_token == value)
return;
_token = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Token)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsSigned)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsNotSigned)));
}
}
public bool IsSigned => Token != null && Token.ExpirationDate > DateTime.Now;
public bool IsNotSigned => !IsSigned;
}
// This is a sample. Fille information (email, etc.) can depend on scopes
[DataContract]
public class OAuthToken
{
[DataMember(Name = "access_token")]
public string AccessToken { get; set; }
[DataMember(Name = "token_type")]
public string TokenType { get; set; }
[DataMember(Name = "expires_in")]
public int ExpiresIn { get; set; }
[DataMember(Name = "refresh_token")]
public string RefreshToken { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Picture { get; set; }
[DataMember]
public string Locale { get; set; }
[DataMember]
public string FamilyName { get; set; }
[DataMember]
public string GivenName { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public string Profile { get; set; }
[DataMember]
public string[] Scopes { get; set; }
// not from google's response, but we store this
public DateTime ExpirationDate { get; set; }
}
// largely inspired from
// https://github.com/googlesamples/oauth-apps-for-windows
public sealed class OAuthRequest
{
// TODO: this is a sample, please change these two, use your own!
private const string ClientId = "581786658708-elflankerquo1a6vsckabbhn25hclla0.apps.googleusercontent.com";
private const string ClientSecret = "3f6NggMbPtrmIBpgx-MK2xXK";
private const string AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
private const string TokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
private const string UserInfoEndpoint = "https://www.googleapis.com/oauth2/v3/userinfo";
private OAuthRequest()
{
}
public string AuthorizationRequestUri { get; private set; }
public string State { get; private set; }
public string RedirectUri { get; private set; }
public string CodeVerifier { get; private set; }
public string[] Scopes { get; private set; }
// https://developers.google.com/identity/protocols/OAuth2InstalledApp
public static OAuthRequest BuildLoopbackRequest(params string[] scopes)
{
var request = new OAuthRequest
{
CodeVerifier = RandomDataBase64Url(32),
Scopes = scopes
};
string codeChallenge = Base64UrlEncodeNoPadding(Sha256(request.CodeVerifier));
const string codeChallengeMethod = "S256";
string scope = BuildScopes(scopes);
request.RedirectUri = string.Format("http://{0}:{1}/", IPAddress.Loopback, GetRandomUnusedPort());
request.State = RandomDataBase64Url(32);
request.AuthorizationRequestUri = string.Format("{0}?response_type=code&scope=openid%20profile{6}&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}",
AuthorizationEndpoint,
Uri.EscapeDataString(request.RedirectUri),
ClientId,
request.State,
codeChallenge,
codeChallengeMethod,
scope);
return request;
}
// https://developers.google.com/identity/protocols/OAuth2InstalledApp Step 5: Exchange authorization code for refresh and access tokens
public OAuthToken ExchangeCodeForAccessToken(string code)
{
if (code == null)
throw new ArgumentNullException(nameof(code));
string tokenRequestBody = string.Format("code={0}&redirect_uri={1}&client_id={2}&code_verifier={3}&client_secret={4}&scope=&grant_type=authorization_code",
code,
Uri.EscapeDataString(RedirectUri),
ClientId,
CodeVerifier,
ClientSecret
);
return TokenRequest(tokenRequestBody, Scopes);
}
// this is not used in this sample, but can be used to refresh a token from an old one
// https://developers.google.com/identity/protocols/OAuth2InstalledApp Refreshing an access token
public OAuthToken Refresh(OAuthToken oldToken)
{
if (oldToken == null)
throw new ArgumentNullException(nameof(oldToken));
string tokenRequestBody = string.Format("refresh_token={0}&client_id={1}&client_secret={2}&grant_type=refresh_token",
oldToken.RefreshToken,
ClientId,
ClientSecret
);
return TokenRequest(tokenRequestBody, oldToken.Scopes);
}
private static T Deserialize<T>(string json)
{
if (string.IsNullOrWhiteSpace(json))
return default(T);
return Deserialize<T>(Encoding.UTF8.GetBytes(json));
}
private static T Deserialize<T>(byte[] json)
{
if (json == null || json.Length == 0)
return default(T);
using (var ms = new MemoryStream(json))
{
return Deserialize<T>(ms);
}
}
private static T Deserialize<T>(Stream json)
{
if (json == null)
return default(T);
var ser = CreateSerializer(typeof(T));
return (T)ser.ReadObject(json);
}
private static DataContractJsonSerializer CreateSerializer(Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
var settings = new DataContractJsonSerializerSettings
{
DateTimeFormat = new DateTimeFormat("yyyy-MM-dd'T'HH:mm:ss.fffK")
};
return new DataContractJsonSerializer(type, settings);
}
// https://stackoverflow.com/questions/223063/how-can-i-create-an-httplistener-class-on-a-random-port-in-c/
private static int GetRandomUnusedPort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
private static string RandomDataBase64Url(int length)
{
using (var rng = new RNGCryptoServiceProvider())
{
var bytes = new byte[length];
rng.GetBytes(bytes);
return Base64UrlEncodeNoPadding(bytes);
}
}
private static byte[] Sha256(string text)
{
using (var sha256 = new SHA256Managed())
{
return sha256.ComputeHash(Encoding.ASCII.GetBytes(text));
}
}
private static string Base64UrlEncodeNoPadding(byte[] buffer)
{
string b64 = Convert.ToBase64String(buffer);
// converts base64 to base64url.
b64 = b64.Replace('+', '-');
b64 = b64.Replace('/', '_');
// strips padding.
b64 = b64.Replace("=", "");
return b64;
}
private static OAuthToken TokenRequest(string tokenRequestBody, string[] scopes)
{
var request = (HttpWebRequest)WebRequest.Create(TokenEndpoint);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes(tokenRequestBody);
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
var response = request.GetResponse();
using (var responseStream = response.GetResponseStream())
{
var token = Deserialize<OAuthToken>(responseStream);
token.ExpirationDate = DateTime.Now + new TimeSpan(0, 0, token.ExpiresIn);
var user = GetUserInfo(token.AccessToken);
token.Name = user.Name;
token.Picture = user.Picture;
token.Email = user.Email;
token.Locale = user.Locale;
token.FamilyName = user.FamilyName;
token.GivenName = user.GivenName;
token.Id = user.Id;
token.Profile = user.Profile;
token.Scopes = scopes;
return token;
}
}
private static UserInfo GetUserInfo(string accessToken)
{
var request = (HttpWebRequest)WebRequest.Create(UserInfoEndpoint);
request.Method = "GET";
request.Headers.Add(string.Format("Authorization: Bearer {0}", accessToken));
var response = request.GetResponse();
using (var stream = response.GetResponseStream())
{
return Deserialize<UserInfo>(stream);
}
}
private static string BuildScopes(string[] scopes)
{
string scope = null;
if (scopes != null)
{
foreach (var sc in scopes)
{
scope += "%20" + Uri.EscapeDataString(sc);
}
}
return scope;
}
// https://developers.google.com/+/web/api/rest/openidconnect/getOpenIdConnect
[DataContract]
private class UserInfo
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "kind")]
public string Kind { get; set; }
[DataMember(Name = "email")]
public string Email { get; set; }
[DataMember(Name = "picture")]
public string Picture { get; set; }
[DataMember(Name = "locale")]
public string Locale { get; set; }
[DataMember(Name = "family_name")]
public string FamilyName { get; set; }
[DataMember(Name = "given_name")]
public string GivenName { get; set; }
[DataMember(Name = "sub")]
public string Id { get; set; }
[DataMember(Name = "profile")]
public string Profile { get; set; }
[DataMember(Name = "gender")]
public string Gender { get; set; }
}
}
}
This code uses an embedded Internet Explorer control, but since Google doesn't support old Internet Explorer versions, it's likely that you also need to add some code to play with IE's compatibility features as described here for example: https://stackoverflow.com/a/28626667/403671
You can put that code in App.xaml.cs, like this:
public partial class App : Application
{
public App()
{
// use code from here: https://stackoverflow.com/a/28626667/403671
SetWebBrowserFeatures();
}
}
Note what the webbrowser will display depends entirely on Google, and it can considerably vary depending on the environment like cookies, language, etc.
I think you are missing the point on what the OAuth is, start by reading this and here is a simple description of the flow.
which will just make sure that user can also login with his Google e-mail-password pair, I don't need a returned token for the further use
There is no api for user/pass validation, and the token is the whole point of the OAuth. A user with existing email should be able to login if indeed you registered your application with google. Also i see no point in prior validation of user/pass, just add the button.

SignalR Web API send message to current user

I have web app project and an angular 2 project.
I would like use SignalR to send message from the server.
Then I found this article about implementing it.
But I don't know how to send message to the current user.
Code for send message C#:
public class EventHub : Hub
{
public async Task Subscribe(string channel)
{
await Groups.Add(Context.ConnectionId, channel);
var #event = new ChannelEvent
{
Name = $"{Context.ConnectionId} subscribed",
Data = new
{
Context.ConnectionId,
ChannelName = channel
}
};
await Publish(#event);
}
public async Task Unsubscribe(string channel)
{
await Groups.Remove(Context.ConnectionId, channel);
var #event = new ChannelEvent
{
Name = $"{Context.ConnectionId} unsubscribed",
Data = new
{
Context.ConnectionId,
ChannelName = channel
}
};
await Publish(#event);
}
public Task Publish(ChannelEvent channelEvent)
{
Clients.Caller.OnEvent(Constants.AdminChannel, channelEvent);
return Task.FromResult(0);
}
public override Task OnConnected()
{
var #event = new ChannelEvent
{
Name = $"{Context.ConnectionId} connected",
Data = new
{
Context.ConnectionId,
}
};
Publish(#event);
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
var #event = new ChannelEvent
{
Name = $"{Context.ConnectionId} disconnected",
Data = new
{
Context.ConnectionId,
}
};
Publish(#event);
return base.OnDisconnected(stopCalled);
}
}
public static class Constants
{
public const string AdminChannel = "admin";
public const string TaskChannel = "tasks";
}
public class ChannelEvent
{
public string Name { get; set; }
public string ChannelName { get; set; }
public DateTimeOffset Timestamp { get; set; }
public object Data
{
get { return _data; }
set
{
_data = value;
Json = JsonConvert.SerializeObject(_data);
}
}
private object _data;
public string Json { get; private set; }
public ChannelEvent()
{
Timestamp = DateTimeOffset.Now;
}
}
Then in my controller I create IhubContent
private readonly IHubContext _context = GlobalHost.ConnectionManager.GetHubContext<EventHub>();
and invoke my publish event:
private void PublishEvent(string eventName, StatusModel status)
{
_context.Clients.Group(Constants.TaskChannel).OnEvent(Constants.TaskChannel, new ChannelEvent
{
ChannelName = Constants.TaskChannel,
Name = eventName,
Data = status
});
}
But this message sent to all users. Help me to fix this issue and implement code to the send message to the current user.
The IHubContext object your are using has multiple methods, one of which is Clients, of type IHubConnectionContext.
In there you have Groups, Group, Clients & Client methods which abstract what you want to target.
In your case using:
_context.Clients.Client(connectionId).send(message);
should be working fine (you still need to propagate the connectionId though).
N.B.: send is a JS method that should be implemented on client-side.

WebRequest 400 bad request to api

I am making webrequest to instagram api, and recieving 400 bad request, but i guess this is not something specific to instagram and is a generic error in my code.
here is my code."code" paramter in this request is obtained from previous request.Code breaks at "GetResponse" method
string clientID = "myclientid";
string clientsecret="myclientsecret";
string redirectURL = "http://localhost:64341/";
string webrequestUri = "https://api.instagram.com/oauth/
access_token? client_id=" + clientID + "&client_secret=
" + clientsecret + "&grant_type=authorization_code" + "&redirect_uri="redirectURL+"&code="
+Request.QueryString["code"].ToString();
WebRequest request = WebRequest.Create(webrequestUri);
request.Method = "POST";
WebResponse response = request.GetResponse();
C#/asp.net Working example:
namespace InstagramLogin.Code
{
public class InstagramAuth
{
private InstagramClient myApp = new InstagramClient();
public string genUserAutorizationUrl()
{
return String.Format(myApp.OAuthAutorizeURL, myApp.ClientId, myApp.RedirectUri);
}
public AuthUserApiToken getTokenId(string CODE)
{
//curl \-F 'client_id=CLIENT-ID' \
//-F 'client_secret=CLIENT-SECRET' \
//-F 'grant_type=authorization_code' \
//-F 'redirect_uri=YOUR-REDIRECT-URI' \
//-F 'code=CODE' \https://api.instagram.com/oauth/access_token
var wc = new WebClient();
var wcResponse = wc.UploadValues(myApp.AuthAccessTokenUrl,
new System.Collections.Specialized.NameValueCollection() {
{ "client_id", myApp.ClientId },
{ "client_secret", myApp.ClientSecret },
{ "grant_type", "authorization_code"},
{ "redirect_uri", myApp.RedirectUri},
{ "code", CODE}
});
var decodedResponse = wc.Encoding.GetString(wcResponse);
AuthUserApiToken UserApiToken = JsonConvert.DeserializeObject<AuthUserApiToken>(decodedResponse);
return UserApiToken;
}
}
}
your object:
namespace InstagramLogin.Code
{
public class InstagramClient
{
private const string ApiUriDefault = "https://api.instagram.com/v1/";
private const string OAuthUriDefault = "https://api.instagram.com/oauth/";
private const string RealTimeApiDefault = "https://api.instagram.com/v1/subscriptions/";
private const string OAuthAutorizeURLDefault = "https://api.instagram.com/oauth/authorize/?client_id={0}&redirect_uri={1}&response_type=code";
private const string AuthAccessTokenUrlDefault = "https://api.instagram.com/oauth/access_token";
private const string clientId = "clientid";
private const string clientSecretId = "clientsecretid";
private const string redirectUri = "http://localhost/InstagramLogin/InstaAuth.aspx";
private const string websiteUri = "http://localhost/InstagramLogin/InstaAuth.aspx";
public string ApiUri { get; set; }
public string OAuthUri { get; set; }
public string RealTimeApi { get; set; }
public string OAuthAutorizeURL { get { return OAuthAutorizeURLDefault; } }
public string ClientId { get { return clientId; } }
public string ClientSecret { get { return clientSecretId; } }
public string RedirectUri { get { return redirectUri; } }
public string AuthAccessTokenUrl { get { return AuthAccessTokenUrlDefault; } }
//removed props
}
}
instagram loged user:
namespace InstagramLogin.Code
{
public class SessionInstaAuthUser
{
public bool isInSession()
{
return HttpContext.Current.Session["AuthUserApiToken"] != null;
}
public AuthUserApiToken get()
{
if (isInSession())
{
return HttpContext.Current.Session["AuthUserApiToken"] as AuthUserApiToken;
}
return null;
}
public void set(AuthUserApiToken value)
{
HttpContext.Current.Session["AuthUserApiToken"] = value;
}
public void clear()
{
if (isInSession())
{
HttpContext.Current.Session["AuthUserApiToken"] = null;
}
}
}
}
markup:
<asp:Button ID="btnInstaAuth"
Text="Click here to get instagram auth"
runat="server" />
Code behind:
//objects
private InstagramClient InstagramApiCLient = new InstagramClient();
private InstagramAuth AuthManager = new InstagramAuth();
private SessionInstaAuthUser SesssionInstaUser = new SessionInstaAuthUser();
//click login with tests - user is logged (in session)
void btnInstaAuth_Click(Object sender,
EventArgs e)
{
btnGetInstaPosts.Visible = false;
if (SesssionInstaUser.isInSession())
{
SesssionInstaUser.clear();
Button clickedButton = (Button)sender;
clickedButton.Text = "Click here to get instagram auth";
btnInstaAuth.Visible = true;
}
else
{
Response.Redirect(AuthManager.genUserAutorizationUrl());
}
}
You can find what you need in this class InstagramAuth sorry if I did forgot to remove some of extra methods or properties, please strip it out.
This button can be used on all page, still don't forget to add on page load at the page set in instagram as login page, query string parse:
//here you read the token instagram generated and append it to the session, or get the error :)
if (!IsPostBack)
{
if (!SesssionInstaUser.isInSession())
{
if (Request.QueryString["code"] != null)
{
var token = AuthManager.getTokenId(Request.QueryString["code"]);
SesssionInstaUser.set(token);
//set login button to have option to log out
btnInstaAuth.Text = token.user.username + " leave instagtam oAuth";
Response.Redirect(Request.Url.ToString().Split('?')[0]);
}
else
if (Request.QueryString["error"] != null)
{
btnInstaAuth.Text = Request.QueryString["error_description"];
}
}
}
Sorry If I'd missed something, php curl in to c# is implemented in first class.
Update (I did forget something), insta user obj :)
namespace InstagramLogin.Code
{
public class UserLogged
{
public string id { get; set; }
public string username { get; set; }
public string full_name { get; set; }
public string profile_picture { get; set; }
}
public class AuthUserApiToken
{
public string access_token { get; set; }
public UserLogged user { get; set; }
}
}

Categories

Resources