Created
January 22, 2017 17:02
-
-
Save cihanyakar/019840d8357bbaf4313051cdf59242ae to your computer and use it in GitHub Desktop.
Gerekli Sınıflar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using Xamarin.Forms; | |
namespace CloudPrint.Clients | |
{ | |
public sealed class GoogleCloudPrintClient | |
{ | |
private readonly HttpClient _httpClient; | |
private readonly string _clientId; | |
private readonly string _clientSecret; | |
public GoogleCloudPrintClient(string clientId, string clientSecret) | |
{ | |
_clientId = clientId; | |
_clientSecret = clientSecret; | |
_httpClient = new HttpClient { BaseAddress = new Uri("https://www.google.com/cloudprint/") }; | |
} | |
public void GirisYap() | |
{ | |
// Google ın login sayfasını gösterecek bir web view ayarlıyorum | |
var webSayfasi = new WebView | |
{ | |
HorizontalOptions = LayoutOptions.FillAndExpand, | |
VerticalOptions = LayoutOptions.FillAndExpand, | |
// scope : benim istediğim yetkileri belirtiyor. | |
// client_id : uygulamamı belirtiyor. | |
// response_type : geriye code dönmesini istediğimi belirtiyor. | |
// redirect_uri : giriş yapılınca yönlenmesini istediğim sayfayı belirtiyor. | |
Source = | |
$"https://accounts.google.com/o/oauth2/v2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloudprint&response_type=code&client_id={_clientId}&redirect_uri=http%3A%2F%2Flocalhost%3A80" | |
}; | |
//Uygulamamın aktif sayfasını değiştiriyorum | |
var mevcutSayfa = Application.Current.MainPage; | |
Application.Current.MainPage = new ContentPage { Content = webSayfasi }; | |
//web view adresi değiştiğinde | |
webSayfasi.Navigated += async (s, e) => | |
{ | |
// adres bu bilgiliyi içeriyorsa login başarılıdır, başarılı olmayan senaryoları size bırakıyorum | |
if (e.Url.Contains("?code=")) | |
{ | |
// ihtiyacım olan kod URL içinde, string i parçalayıp alıyorum | |
var kod = e.Url.Substring(e.Url.LastIndexOf("?code=", StringComparison.Ordinal) + 6); | |
// web view içinde yönlendirme hatası gözükmemesi için gizliyorum | |
webSayfasi.Source = new HtmlWebViewSource { Html = @"<html><body> <h1Bekleyiniz</h1> </body></html>" }; | |
// tek kullanımlık bir http client açıyorum, tek kullanımlık olduğundan işim bittiği anda dispose ediyorum | |
using (var tokenClient = new HttpClient()) | |
{ | |
// elimdeki kod ve uygulamamın gizli anahtarı ile yetkilendirme işini yapıyorum | |
// bana token dönecek, | |
var requestContent = new FormUrlEncodedContent(new Dictionary<string, string> | |
{ | |
{"code",kod }, | |
{"client_id",_clientId }, | |
{"client_secret", _clientSecret }, | |
{"redirect_uri","http://localhost:80" }, | |
{"grant_type","authorization_code" }, | |
}); | |
var yanit = await tokenClient.PostAsync("https://www.googleapis.com//oauth2/v4/token", requestContent); | |
var yanitIcerigi = await yanit.Content.ReadAsStringAsync(); | |
// dönen bilgiyi json dan dictionary'e çevirip token kısmını alıyorum | |
var token = JsonConvert.DeserializeObject<IDictionary<string, string>>(yanitIcerigi)["access_token"]; | |
// bundan sonraki tüm isteklerimde bu token ı kullanacağım | |
_httpClient.DefaultRequestHeaders.Authorization = | |
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); | |
} | |
// Login ekranını göstermeden önceki sayfaya dönüyorum | |
Application.Current.MainPage = mevcutSayfa; | |
} | |
}; | |
} | |
public async Task<List<Printer>> YazicilariGetirAsync() | |
{ | |
if (_httpClient.DefaultRequestHeaders.Authorization == null) | |
{ | |
GirisYap(); | |
return null; | |
} | |
var yanitIcerigi = await _httpClient.GetStringAsync("search"); | |
return JsonConvert.DeserializeObject<AramaSonucu>(yanitIcerigi).Printers.ToList(); | |
} | |
public async Task<bool?> YazdirAsync(Printer yazici, string yazi) | |
{ | |
if (_httpClient.DefaultRequestHeaders.Authorization == null) | |
{ | |
GirisYap(); | |
return null; | |
} | |
var requestContent = new FormUrlEncodedContent(new Dictionary<string, string> | |
{ | |
{"printerid",yazici.Id }, | |
{"title", "Xamarin TR" }, | |
{"ticket", JsonConvert.SerializeObject(new | |
{ | |
version = "1.0", | |
print = new | |
{ | |
color = new {type="STANDARD_MONOCHROME"}, | |
copies = new {copies = 1} | |
}, | |
},Newtonsoft.Json.Formatting.Indented)}, | |
{"content", yazi }, | |
{"contentType","text/plain" }, | |
}); | |
using (var yanit = await _httpClient.PostAsync("submit", requestContent)) | |
{ | |
return yanit.IsSuccessStatusCode; | |
} | |
} | |
} | |
public class Params | |
{ | |
[JsonProperty("q")] | |
public IList<string> Q { get; set; } | |
[JsonProperty("connection_status")] | |
public IList<string> ConnectionStatus { get; set; } | |
[JsonProperty("use_cdd")] | |
public IList<string> UseCdd { get; set; } | |
[JsonProperty("extra_fields")] | |
public IList<string> ExtraFields { get; set; } | |
[JsonProperty("type")] | |
public IList<string> Type { get; set; } | |
} | |
public class Request | |
{ | |
[JsonProperty("time")] | |
public string Time { get; set; } | |
[JsonProperty("params")] | |
public Params Params { get; set; } | |
[JsonProperty("user")] | |
public string User { get; set; } | |
[JsonProperty("users")] | |
public IList<string> Users { get; set; } | |
} | |
public class Current | |
{ | |
[JsonProperty("xmpp_timeout_value")] | |
public int XmppTimeoutValue { get; set; } | |
} | |
public class LocalSettings | |
{ | |
[JsonProperty("current")] | |
public Current Current { get; set; } | |
} | |
public class Printer | |
{ | |
[JsonProperty("isTosAccepted")] | |
public bool IsTosAccepted { get; set; } | |
[JsonProperty("displayName")] | |
public string DisplayName { get; set; } | |
[JsonProperty("local_settings")] | |
public LocalSettings LocalSettings { get; set; } | |
[JsonProperty("description")] | |
public string Description { get; set; } | |
[JsonProperty("capsHash")] | |
public string CapsHash { get; set; } | |
[JsonProperty("updateTime")] | |
public string UpdateTime { get; set; } | |
[JsonProperty("type")] | |
public string Type { get; set; } | |
[JsonProperty("ownerId")] | |
public string OwnerId { get; set; } | |
[JsonProperty("tags")] | |
public IList<string> Tags { get; set; } | |
[JsonProperty("gcpVersion")] | |
public string GcpVersion { get; set; } | |
[JsonProperty("proxy")] | |
public string Proxy { get; set; } | |
[JsonProperty("ownerName")] | |
public string OwnerName { get; set; } | |
[JsonProperty("createTime")] | |
public string CreateTime { get; set; } | |
[JsonProperty("defaultDisplayName")] | |
public string DefaultDisplayName { get; set; } | |
[JsonProperty("name")] | |
public string Name { get; set; } | |
[JsonProperty("connectionStatus")] | |
public string ConnectionStatus { get; set; } | |
[JsonProperty("id")] | |
public string Id { get; set; } | |
[JsonProperty("status")] | |
public string Status { get; set; } | |
[JsonProperty("accessTime")] | |
public string AccessTime { get; set; } | |
} | |
public class AramaSonucu | |
{ | |
[JsonProperty("success")] | |
public bool Success { get; set; } | |
[JsonProperty("request")] | |
public Request Request { get; set; } | |
[JsonProperty("printers")] | |
public IList<Printer> Printers { get; set; } | |
[JsonProperty("xsrf_token")] | |
public string XsrfToken { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment