Я не смог найти пакет nuget для диска Google, который поддерживает аутентификацию Xamarin и OAuth. Поэтому я нашел способ сделать это с помощью Http Client Rest API
- Создание идентификатора клиента OAuth с помощью консоли разработчика Google
Используйте идентификатор клиента вместе с другой информацией, такой как перенаправление
public class GoogleAuthenticator : OAuth2Authenticator
{
public static OAuth2Authenticator Auth;
public GoogleAuthenticator(string clientId, string scope, Uri authorizeUrl, Uri redirectUrl,
GetUsernameAsyncFunc getUsernameAsync = null, bool isUsingNativeUi = false) : base(clientId, scope,
authorizeUrl, redirectUrl, getUsernameAsync, isUsingNativeUi)
{
Auth = new OAuth2Authenticator(clientId, string.Empty, scope,
authorizeUrl,
redirectUrl,
new Uri(GoogleDriveConfig.AccessTokenUri),
null, true);
Auth.Completed += OnAuthenticationCompleted;
Auth.Error += OnAuthenticationFailed;
}
public void OnAuthenticationCompleted(object sender,AuthenticatorCompletedEventArgs e)
{
if (e.IsAuthenticated)
{
AuthenticationCompleted?.Invoke(e.Account.Properties["access_token"],
e.Account.Properties["refresh_token"], e.Account.Properties["expires_in"]);
}
}
public void OnAuthenticationFailed(object sender, AuthenticatorErrorEventArgs e)
{
}
Запустите браузер
public void SignInToDrive(Activity context)
{
Intent loginUi = Auth.GetUI(context);
CustomTabsConfiguration.IsShowTitleUsed = false;
CustomTabsConfiguration.IsActionButtonUsed = false;
context.StartActivity(loginUi);
}
Создание действия GoogleAuthInterceptor
[Activity(Label = "GoogleAuthInterceptor")]
[ IntentFilter ( actions: new[]
{Intent.ActionView},
Categories = new[] { Intent.CategoryDefault,
Intent.CategoryBrowsable },
DataSchemes = new[] {"YOUR PACKAGE NAME"}, DataPaths = new[] { "/oauth2redirect"
} ) ]
public class GoogleAuthInterceptor : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Uri uriAndroid = Intent.Data;
System.Uri uri = new System.Uri(uriAndroid.ToString());
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
StartActivity(intent);
GoogleAuthenticator.Auth.OnPageLoading(uri);
Finish();
return;
}
}
Загрузить файл на Google Drive
Этот базовый адрес для всех приведенных ниже API Google Drive *
HttpClient _httpClient = httpClient ?? new HttpClient
{
BaseAddress = new Uri(),
};
private async Task<HttpResponseMessage> CreateResumableSession(string accessToken, string fileName, long fileSize, string folderId)
{
var sessionRequest = new
HttpRequestMessage(HttpMethod.Post,"upload/drive/v3/files?uploadType=resumable");
sessionRequest.Headers.Add("Authorization", "Bearer " + accessToken);
sessionRequest.Headers.Add("X-Upload-Content-Type", "*/*");
sessionRequest.Headers.Add("X-Upload-Content-Length", fileSize.ToString());
string body = "{\"name\": \"" + fileName + "\", \"parents\": [\"" + folderId + "\"]}";
sessionRequest.Content = new StringContent(body);
sessionRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (var sessionResponse = await _httpClient.SendAsync(sessionRequest) )
{
return sessionResponse;
};
Обновление файла на Google Диске
private async Task<bool> UpdateFile(string accessToken,string fileId,long fileSize,Stream stream)
{
HttpRequestMessage sessionRequest = new HttpRequestMessage(new HttpMethod("PATCH"), "upload/drive/v3/files/" + $"{fileId}");
sessionRequest.Headers.Add("Authorization", "Bearer " + accessToken);
sessionRequest.Headers.Add("X-Upload-Content-Type", "*/*");
sessionRequest.Headers.Add("X-Upload-Content-Length", fileSize.ToString());
sessionRequest.Content = new StreamContent(stream);
sessionRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("*/*");
using (var response = await _httpClient.SendAsync(sessionRequest))
{
return response.IsSuccessStatusCode;
}
}
Скачать файл
public async Task<Stream> DownloadFile(string accessToken, string fileId)
{
var request = new HttpRequestMessage(HttpMethod.Get,"drive/v3/files"+ $"/{fileId}" + "?fields=*&alt=media");
request.Headers.Add("Authorization", "Bearer " + accessToken);
var response = await _httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var contents = await response.Content.ReadAsStreamAsync();
return contents;
}
return null;
}
Получить все файлы
public async Task<GoogleDriveItem> GetFiles(string folderId, string accessToken)
{
var request = new HttpRequestMessage(HttpMethod.Get,
"drive/v3/files" +"?q=parents%20%3D%20'" +
$"{folderId}" +
"'%20and%20trashed%20%3D%20false&fields=files(id%2Cname%2Ctrashed%2CmodifiedTime%2Cparents)");
request.Headers.Add("Authorization", "Bearer " + accessToken);
request.Headers.Add("Accept", "application/json");
request.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true
};
using (var response = await _httpClient.SendAsync(request))
{
var content = await response.Content.ReadAsStringAsync();
return (JsonConvert.DeserializeObject<GoogleDriveItem>(content));
}
}