get Google Credential throws Unhandled Exception - c#

On My Xamarin.forms Portable Project, I am trying to read information from google sheet:
using (var stream = this.Assets.Open(#"clientsecret.json"))
{
var secrets = GoogleClientSecrets.Load(stream).Secrets;
//I get the secrets correctly
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath,true)).Result;
}
I get
Unhandled Exception
System.AggregateException: One or more errors occurred.
when the complier trying to get credential, keep in your mind the same code is working fine in windows forms application

You appear to be using the Google APIs .Net client library. At this time the client library does not officially support Xamarin
Please see the issue here Investigate Xamarin support #984 or this one #840
Option 1:
Create a fork of the Google APIs .Net client library and fix any issues you can find. The client library is open source so this should be doable. I am sure we would be happy to accept a pull request if you get it working.
Option 2:
Create your own library for accessing just the sheets api. This may be the faster way to go but you need to have some understanding of how Google oauth works in order to do this.

Sounds like it could be a similar issue I am/was facing with auth2 in a UWP app. I use the same kind of auth flow as per your code, and it throws an exception at runtime when I use the mainstream Google APIs .Net Client library. In my case, I was able to use the beta version of the library v1.31.0 beta 01, and that got my UWP app and the auth flow working fine. From looking at the branch the magic is that the beta libraries will default the FileDataStore to a PasswordVaultDataStore object for UWP, which seems to work fine. There are also other differences like UWP code receiver classes etc but I haven't really checked in detail. For all it's worth, try the beta library and see if it helps in your case.

Related

Azure .NET v4 SDK Proxy Configuration in .NET Framework

I'm using the newer v4.x version of the Azure .NET SDKS - https://www.nuget.org/packages/Azure.Security.KeyVault.Secrets/4.1.0
In my case this is to access Key Vault secrets but the question probably applies globally. When using the above nuget package to retrieve a secret through a .NET Core 3.1 app, all seems to work OK.
var credential = new ClientSecretCredential("<TENANT_ID>", "<CLIENT_ID>", "<SECRET>");
var client = new SecretClient(new Uri("https://MyVault.vault.azure.net/"), credential);
var secret = await client.GetSecretAsync("MySecret");
However I need it to run in a .NET Framework 4.7.1, the call eventually times out after retrying 4 times.
I fully suspect this is down to the corporate proxy I work behind (although if there may be other reasons please tell me).
There is a SecretClientOptions that inherits from ClientOptions which contains a Transport property but it is not immediately obvious how to use this.
I see on other versions of the Node SDK they have a proxyOptions property but that doesn't exist in the .NET version. Is there another way to configure this?
I also wanted to try an diagnose a bit further but am not sure how to use the Diagnostics property and I struggle to get Fiddler to capture any .NET traffic these days.
UPDATE: Looks like a fix for this was committed 9 days ago so expecting a fix soon which will make .NET Framework use the system proxy correctly - https://github.com/Azure/azure-sdk-for-net/issues/16990
After diving into the library code it appears that the normal proxy as used with HttpClient isn't used. By setting the HTTP_PROXY and HTTPS_PROXY environment variables I was able to get this to work.
Environment.SetEnvironmentVariable("HTTP_PROXY", "http://my.corporate.proxy/");
Environment.SetEnvironmentVariable("HTTPS_PROXY", "http://my.corporate.proxy/");
UPDATE: Looks like a fix for this was committed 9 days ago so expecting a fix soon which will make .NET Framework use the system proxy correctly - https://github.com/Azure/azure-sdk-for-net/issues/16990
An alternative solution here can be the answer by Mun here. He suggests to cache the Key Vault values in App Settings Data structure.
You can also try this approach suggested by Docs if you don't prefer above approach.

IBM Watson Unity SDK, IamAuthenticator objects cannot authenticate successfully

I am trying to query Watson Discovery News and get some responses in Unity.
First I tried to authenticate my Watson Discovery service through Discovery service APIKey(As described in the "IAM" section in https://github.com/watson-developer-cloud/unity-sdk). My code is exactly the same as the example code in "Query a collection" section in https://cloud.ibm.com/apidocs/discovery/discovery?code=unity#query-a-collection.
Unfortunately, the code always returns due to unsuccessful authentication, thus I cannot get any response. After some debugging, I found out that "tokenData" field in IamAuthenticator.cs(This is part of IBM Unity SDK) is not initialized; this leads to the unsuccessful authentication.
I was able to debug by changing the "yield return" in example code to "return". Otherwise, C# debugger cannot step into the code.
Since I have followed all the steps in IBM documentations, I am not sure how to proceed.
To reproduce the problem, one has to download IBM Unity SDK on https://github.com/watson-developer-cloud/unity-sdk and follow the instructions in README. After setting up, one could replicate the problem using the code below:
var authenticator = new IamAuthenticator(
apikey: "{apikey}"
);
while (!authenticator.CanAuthenticate())
yield return null;
Thanks in advance for any help!
Have you set the URL (SetServiceUrl) to match the location for your service instance? See Service Endpoint in the API reference
I bypassed this problem using Token authentication instead of using APIkey.
If one has the choice of using other SDKs, e.g. Java SDK, please do so. Because Watson Unity SDK is not well supported at the moment.

How to call Microsoft Graph Beta API from C#

I tried to get the content of the user's profile picture and I found out that I had to call the Beta version because the current version gives the following error message:
"code": "GetUserPhoto",
"message": "The operation is not supported."
So, I tried to switch to Beta, and here is the code that I wrote in C# to do it, but it doesn't work:
Microsoft.Graph 1.6.2
List<QueryOption> options = new List<QueryOption>
{
new QueryOption("$api-version", "beta")
};
var pictureStream = await graphClient.Me.Photo.Content.Request(options).GetAsync();
I got the same error message.
I tried the same request in the Graph Explorer. The 1.0 doesn't work, but Beta works.
The api-version query parameter is used by the Azure AD Graph API. This is a different API than Microsoft Graph. There is a lot of functional overlap (Azure AD Graph is slowly being migrated over to Microsoft Graph) but they use entirely different entities and calling conventions.
In order to call the /beta endpoint using the Microsoft Graph .NET Client Library, you need to change the BaseUrl of the client:
graphClient.BaseUrl = "https://graph.microsoft.com/beta";
var pictureStream = await graphClient.Me.Photo.Content.Request().GetAsync();
Some important notes about the /beta endpoint:
It isn't supported and isn't suitable for production. So don't do that. Or at least don't tell anyone and don't call Support if it stops working. ;-)
The .NET Client uses objects constructed off the production metadata. This means that any entities, actions or properties that were added in /beta don't exist in the models shipped with the SDK.
The .NET Client will ignore any values returned by Microsoft Graph that it doesn't expect to see. So if an endpoint returns a property that wasn't included in the production metadata (see #2), it will simply be ignored.
So long as you're only using a /beta to gain functionality but still expecting /v1.0 results, it should work okay. Photos for example only look at Exchange in v1.0 but look in both Exchange and Active Directory but still return the same result. In theory this means you should be able to swap /beta for /v1.0 without a problem.
I think you are still calling V1 endpoint. In fact, the Beta endpoint is not currently supported in the Microsoft Graph .NET Client Library. More info here.
There is an official beta client for Graph API now:
https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet

Twitter library for metro app (still maintained)

I'm looking for a Twitter library to use Twitter API in a metro app ... and I am hitting a wall.
So far, here is what I found :
Twitterizer : Project closed, no version for metro apps
TweetSharp : Project closed, no version for metro apps
TwitterRT : Working for Metro app, I'm currently using it in my application, but not maintained anylonger, and not working for Twitter API 1.1 so I get 401 errors since Twitter API 1.0 is closed
Linq2Twitter : It seems to be easier to create a new library from scratch than to use this one.
If any one knows a good existing library, or a clear tutorial to use Linq2Twitter (the one I find are not clear about how to connect with Oauth), I would be very glad.
PS : If it helps, the purpose of my application is to post a status on behalf of a user.
I found a solution by using Twitter RT and editing the source code to work with API 1.1
First I needed to change the updateStatusUrl constant :
const string _updateStatusUrl = "https://api.twitter.com/1.1/statuses/update.json";
Then, in the PostData method, I had to explicitly specify the request mime type :
Request.ContentType = "application/x-www-form-urlencoded";
This worked for me.
I hope it will help people with the same problem.

C# Amazon Product Advertising API

As of August 15, Amazon made it compulsory to sign all requests made to their Product Advertising API. I thought I had got everything working just fine but when the 15th finally came around, my web application stopped working and pretty much ever since I have been trying to find out how to sign the SOAP requests.
Amazon has an outdated sample code for signing requests that doesn't appear to work here
Basically, I need to know how to add a signature to the my requests using the most current C# SOAP API and .NET 3.5.
I hope I have given enough details, if I haven't please feel free to ask me to elaborate.
Thank You
The_Lorax
UPDATE:
I am using MVC and need to know how to add the Signature to the the ItemLookup or AWSECommerceService object. Is there an attribute that contains the signature value? How does it get attached to the request?
On this page, they say that I must include the Signature and TimeStamp parameters but the intellisense does now show any such attributes.
Check out http://flyingpies.wordpress.com/2009/08/01/17/. It has a walkthrough and a sample visual studio solution using C#, SOAP, WCF on .NET 3.5.
This library automatic sign the requests (Install-Package Nager.AmazonProductAdvertising)
https://www.nuget.org/packages/Nager.AmazonProductAdvertising/
Example:
var authentication = new AmazonAuthentication("accesskey", "secretkey");
var client = new AmazonProductAdvertisingClient(authentication, AmazonEndpoint.US);
var result = await client.SearchItemsAsync("canon eos");

Categories

Resources