Can't find toChannelCredentials() in C# for Google API Authentication - c#

On this page, https://cloud.google.com/docs/authentication/production, there is a guide with examples on how to set up google API authentication. However, my IDE doesn't seem to understand or be able to find the "ToChannelCredentials()" method needed for verification. Is there something specific I haven't imported? Here is my code
using System;
using System.IO;
using Grpc.Core;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
namespace ClassLibrary1
{
public class Class1
{
public static void Main(String[] args)
{
var credential = GoogleCredential.FromFile("myPath").CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.;
var client = ImageAnnotatorClient.Create();
var image = Image.FromFile("myImage");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
{
Console.WriteLine(annotation.Description);
}
}
}
}
}

Add:
using Grpc.Auth;
And you'll need to reference Google.Apis.Core.

Related

Redis Crashing in .Net 6

I have a weird error in Redis on .Net 6. When I run the test code here:
https://github.com/redis-developer/redis-graph-dotnet-basic-app/blob/main/Program.cs
It works perfectly fine. In this case the code is running in the program.cs file.
When I port that code to a class, in order to better manage encapsulation and complexity. It does not work. What it does is run the code and when it gets to the: await graph.QueryAsync part, it just stops the debugger. Very strange indeed.
Here is the code I am using. Any thoughts or suggestions:
//Program.cs (Relevant Bits)
using RedisTest //PROGRAM //WRITE TO REDIS ENTERPRISE CLOUD Process_LoadGraph process_LoadGraph = new Process_LoadGraph(); process_LoadGraph.Controller(results);
//SHARED CONNECTION CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace RedisTest
{
public class RedisSharedConnection
{
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(ConfigData.dbConnectionString);
return connectionMultiplexer;
});
}
}
//USAGE CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NRedisGraph;
namespace RedisTest
{
public class Process_LoadGraph
{
public async void Controller(List<Result> results)
{
//Setup
var redisConnection = RedisSharedConnection.Connection;
//var redisConnection = ConnectionMultiplexer.Connect(ConfigData.dbConnectionString);
var db = redisConnection.GetDatabase(ConfigData.dbId);
var graph = new RedisGraph(db);
string graphName = ConfigData.graphName;
//Test Transaction
// Create Bob
// CRASHES HERE
var createBobResult = await graph.QueryAsync("pets", "MERGE(:human{name:'Bob',age:32})");
}
}
}
Turns out the solution is to use Redis in a static class. Along the following lines:
internal static class WriteToDB
{
public static async Task WriteAsync(List<string> querieS)
{
//Load Graph
//Setup
var redisConnection = RedisSharedConnection.Connection;
//var redisConnection = ConnectionMultiplexer.Connect(ConfigData.dbConnectionString);
var db = redisConnection.GetDatabase(ConfigData.dbId);
var graph = new RedisGraph(db);
string graphName = ConfigData.graphName;
// ** DEBUG
//Test Transaction
// Create Bob
var createBobResult = await graph.QueryAsync("pets", "MERGE(:human{name:'Bob',age:32})");
{ }
//Clear Graph
await graph.QueryAsync(graphName, "MATCH(n) DETACH DELETE n");
{ }
}
}

PlatformChecks.RequestPermissions is not available

I am using xamamrin for developing apps for android using c#, the IDE I am using is Visual studio.
I want to use zxing in my app, so I add zxing packet to both core and ui layers.
As you see in the below code, I can instantiate some classes, but I do not have any access to
PlatformChecks.IsPermissionInManifest
I need it to check weather the required permissions are granted or not. Please have a look at the error message I am getting
Please let me know how to use it.
error
Error CS0103: The name 'PlatformChecks' does not exist in the current context (CS0103) (MITScan.UI.Droid)
code:
public static readonly string[] RequiredPermissions = new[] {
Android.Manifest.Permission.Camera
};
protected override async void OnCreate(Android.OS.Bundle bundle)
{
base.OnCreate(bundle);
this.Window.AddFlags(WindowManagerFlags.KeepScreenOn);
SetContentView(Resource.Layout.ZxingLibTestView);
_scannerFragment = new ZXingScannerFragment();
SupportFragmentManager.BeginTransaction().Replace(Resource.Id.tscan_4_view_relativelayout_cameraPreview, _scannerFragment, "Scannerfragment").Commit();
var permissionsToRequest = new List<string>();
// Check and request any permissions
foreach (var permission in RequiredPermissions)
{
if (PlatformChecks.IsPermissionInManifest(this,
permission))
{
if (!PlatformChecks.IsPermissionGranted(this,
permission))
permissionsToRequest.Add(permission);
}
}
if (permissionsToRequest.Any())
{
_waitingForPermission =
PlatformChecks.RequestPermissions(this,
permissionsToRequest.ToArray(), 101);
}
}
Note:
I am using Zxing for scanning bar codes.
I was going through the Zebra Crossing's(ZXing) docs after I read this question since I knew there is no such Android class and found that PlatformChecks is a Custom Class as below:
public class PlatformChecks
{
public const string PERMISSION_CAMERA = "android.permission.CAMERA";
public const string PERMISSION_FLASHLIGHT = "android.permission.FLASHLIGHT";
public static bool HasCameraPermission(Context context)
{
return HasPermission (context, PERMISSION_CAMERA);
}
public static bool HasFlashlightPermission(Context context)
{
return HasPermission (context, PERMISSION_FLASHLIGHT);
}
static bool HasPermission(Context context, string permission)
{
PermissionInfo pi = null;
try { pi = context.PackageManager.GetPermissionInfo (PERMISSION_CAMERA, PackageInfoFlags.Permissions); }
catch { }
return pi != null;
}
}
And has the following using's
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Content.PM;

Xamarin.Forms Connecting to Web Services

Good Day Everyone. I'm creating a simple Xamarin.Forms Portable Application in my Visual Studio 2015.
I want my Mobile Application to connect to the SQL Database I have in my VS2015 and return a LIST OF CUSTOMERS which should be display to my mobile phone.
I have created a Xamarin Portable project and a WebForms project that will handle my Web Services and Database.
In my WebForms project, I created a Controller that should return the List of Customers. This has a web service URL api/Customer that I used to connect to the RestClient in my Xamarin Portable. I also have CustomerViewModel that should represent the data in my application.
In my Xamarin Portable project, I have a ClientList.xaml that should display the List that comes from my database. I also have a CustomerVM that is connected to Services and my RestClient. My RestClient used the WEB SERVICE URL to get the List of Customer from my WebForms project.
Based on the given steps above, I still wasn't able to display the Data in my mobile phone. What do you think is the reason behind this? Thanks for your help.
Here are some of my codes:
RestClient.cs
public class RestClient_Customer<T>
{
private const string WebServiceUrl = "http://localhost:50857/api/Customer/";
public async Task<List<T>> GetCustomerAsync()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var json = await httpClient.GetStringAsync(WebServiceUrl);
var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
return taskModels;
}
}
CustomerServices.cs
using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
namespace XamarinFormsDemo.Services
{
public class CustomerServices
{
public async Task<List<Customer>> GetCustomerAsync()
{
RestClient_Customer<Customer> restClient = new RestClient_Customer<Customer>();
var customerList = await restClient.GetCustomerAsync(); //yung getasync ay pantawag as restclient
return customerList;
}
}
}
CustomerVM.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
using XamarinFormsDemo.Views;
namespace XamarinFormsDemo.ViewModels
{
public class CustomerVM : INotifyPropertyChanged
{
private List<Customer> _customerList; // keep all customers
private List<Customer> _searchedCustomerList; // keep a copy for searching
private Customer _selectedCustomer = new Customer();
private string _keyword = "";
public string Keyword
{
get
{
return _keyword;
}
set
{
this._keyword = value;
// while keyword changed we filter Employees
//Filter();
}
}
private void Filter()
{
if (string.IsNullOrWhiteSpace(_keyword))
{
CustomerList = _searchedCustomerList;
}
else
{
// var lowerKeyword = _keyword.ToLower();
CustomerList = _searchedCustomerList.Where(r => r.CUSTOMER_NAME.ToLower().Contains(_keyword.ToLower())).ToList();
// EmployeesList = _searchedEmployeesList.Where(r => r.EMPLOYEE_NAME.Contains(_keyword)).ToList();
}
}
public List<Customer> CustomerList
{
get
{
return _customerList;
}
set
{
_customerList = value;
OnPropertyChanged();
}
}
public CustomerVM()
{
InitializeDataAsync();
}
private async Task InitializeDataAsync()
{
var customerServices = new CustomerServices();
_searchedCustomerList = await customerServices.GetCustomerAsync();
CustomerList = await customerServices.GetCustomerAsync();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I think the problem is in your services hope this will help you,
public interface ICustomer
{
Task<string> GetCustomers();
}
public class CustomerService : ICustomer
{
public async Task<string> GetCustomers()
{
var client = new HttpClient();
var response = await client.GetAsync(string.Format("http://mysite/api/Customer"));
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
}
Call it anywhere you like
var _custList = new GetCustomers();
var returnJson = await _custList.GetCustomers();
Note the return is json string format or xml format depending on your REST API so you need to parse this first before you can get the value and display it to ListView
Try running it in UWP. If it works in UWP then you have to take a look at
Xamarin HttpClient.GetStringAsync not working on Xamarin.Droid
I had the same issue but when I tried it in UWP it worked fine. I am still seeking for the solution to run xamarin.android using device.

Flurl & HttpTest: Unit tests fail when Run All, but pass when run individually

Update: HttpTest is not thread-safe, as per the project's GitHub issue. Until the issue is resolved, tests using HttpTest cannot be run in parallel.
I have a really weird pair of test utilizing Flurl and xUnit that, when Run All in VS Test Explorer, will fail, but if run individually, will pass. I cannot for the life of me see anywhere where the 2 are even related to each other, but they do.
I have extracted them out of my project into a new project and the problem persists. I bundled them into a 7z for anyone interested in loading it to VS, but the full code follows.
Project.Commons
GetApi1:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace Project.Commons
{
public class GetApi1
{
public async Task<string> ExecuteAsync(string token)
{
string apikeyKeyname = "token";
dynamic response = await "http://www.api.com"
.SetQueryParams(new { token = token })
.GetJsonAsync();
string receivedApiKey = ((IDictionary<string, object>)response)[apikeyKeyname].ToString();
return receivedApiKey;
}
}
}
GetApi2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace Project.Commons
{
public class GetApi2
{
public async Task<IList<string>> ExecuteAsync()
{
var responses = await "http://www.api.com"
.GetJsonAsync<List<string>>();
var result = new List<string>();
foreach (var response in responses)
{
result.Add("refined stuff");
}
return result;
}
}
}
Project.Tests
UnitTest1:
using Project.Commons;
namespace Project.Tests
{
public class UnitTest1
{
private ITestOutputHelper output;
public UnitTest1(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public async Task ShouldBeAbleToGetApiKeyFromToken()
{
// Arrange
using (var httpTest = new HttpTest())
{
var jsonResponse = new { token = "abcdef" };
string expectedApiKey = "abcdef";
httpTest.RespondWithJson(jsonResponse);
var api = new GetApi1();
// Act
var receivedApiKey = await api.ExecuteAsync("mockToken");
output.WriteLine("Received apikey = " + receivedApiKey);
// Assert
Assert.Equal(expectedApiKey, receivedApiKey);
}
}
}
}
UnitTest2
using Flurl.Http.Testing;
using Project.Commons;
using Xunit;
using Xunit.Abstractions;
namespace Project.Tests
{
public class UnitTest2
{
#region Mock API JSON Response
private IList<string> mockResponse = new List<string>()
{
"raw stuff", "raw stuff", "raw stuff"
};
#endregion
#region Expected Result
private IList<string> expectedResult = new List<string>()
{
"refined stuff", "refined stuff", "refined stuff"
};
#endregion
[Fact]
public async Task CanGetProjectsByWeek()
{
// Arrange
using (var httpTest = new HttpTest())
{
httpTest.RespondWithJson(mockResponse);
// Act
var api = new GetApi2();
var actualResult = await api.ExecuteAsync();
// Assert
Assert.Equal(expectedResult,actualResult);
}
}
}
}
The comments are correct - lack of thread safety is a known limitation of HttpTest. It is logged and under investigation. Parallel testing is much more prevalent today than just a couple years ago when this was created, so while a fix is not trivial, we are treating it with high priority.

Google Drive "get_error not implemented" error

I'm trying to implement the use of Google Drive in my app but I seem to be getting the following error "Method 'get_Error' in type 'Google.Apis.Drive.v2.Data.FileList' from assembly 'Google.Apis.Drive.v2, Version=1.2.4647.29713, Culture=neutral, PublicKeyToken=null' does not have an implementation". Does anyone know as to why this is occurring? I based my code on the example that Google provides for its tasks API.
Code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Util;
using System.Diagnostics;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Util;
using PrepHub.PrepHub;
using System.Web.Services;
using System.Threading;
using Google.Apis;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Drive.v2;
using Google.Apis.Drive;
namespace DriveExample
{
public partial class GDrive : System.Web.UI.Page
{
private static DriveService _service; // We don't need individual service instances for each client.
private OAuth2Authenticator<WebServerClient> _authenticator;
private IAuthorizationState _state;
private IAuthorizationState AuthState
{
get
{
return _state ?? HttpContext.Current.Session["AUTH_STATE"] as IAuthorizationState;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (_service == null)
{
_service = new DriveService(_authenticator = CreateAuthenticator());
}
if (HttpContext.Current.Request["code"] != null)
{
_authenticator = CreateAuthenticator();
_authenticator.LoadAccessToken();
}
var ni = _service.Files.List().Fetch();
}
private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
{
var provider = new WebServerClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = ClientCredentials.ClientID;
provider.ClientSecret = ClientCredentials.ClientSecret;
var authenticator =
new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization) { NoCaching = true };
return authenticator;
}
private IAuthorizationState GetAuthorization(WebServerClient client)
{
// If this user is already authenticated, then just return the auth state.
IAuthorizationState state = AuthState;
if (state != null)
{
return state;
}
// Check if an authorization request already is in progress.
state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
{
// Store and return the credentials.
HttpContext.Current.Session["AUTH_STATE"] = _state = state;
return state;
}
string scope = DriveService.Scopes.Drive.GetStringValue();
OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
response.Send();
return null;
}
}
}
I'm guessing some of your assemblies are out of date. That error will occur when you have an assembly for example, lets say foo.dll (v1) and that assembly is being referenced by bar.dll (v2). A class in bar.dll is expecting something in to be present on a class in Foo and its not there. In your case, its the get accessor for a property called Error on the class FileList. Double check all your assemblies to make sure they are all at their most recent version (s).

Categories