Google Drive API - Example code is not working - asp.net

I want to use Google API to transfer SharePoint Documents to Google Drive using dot net. For that to happen I want to use this link Google Quick Start.
I have followed every little piece of information. It states in beginning "Complete the steps described in the rest of this page, and in about five minutes you'll have a simple Drive app that uploads a file to Google Drive" but it is not true.
I am trying to run this sample example since yesterday but failed.
// Register the authenticator and create the service
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
So it seems that this code example is outdated and Google APIs have been upgraded.
Here comes the warning
[Obsolete("GoogleAuthenticationServer is not supported any more and it's going to be removed in 1.7.0-beta. Consider using the new Google.Apis.Auth NuGet package which supports .NET 4, .NET for Windows Store apps, Windows Phone 7.5 and 8 and Portable Class Libraries as well")]
Another code
var service = new DriveService(new BaseClientService.Initializer()
{
Authenticator = auth
});
And the error
Cannot implicitly convert type 'Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient' to 'Google.Apis.Http.IConfigurableHttpClientInitializer'. An explicit conversion exists (are you missing a cast?)
Then I was looking for [latest release samples] there I found the source files.
I was not able able to run the sample also.
Here I was successfully authenticated but then had error for redirect Uri
My Client_Secrets.JSON looks like, as you can see I have set default redirect Uri to http://localhost/.
{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"secret","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":email","redirect_uris":["http://localhost/"],"client_x509_cert_url":"aa#developer.gserviceaccount.com","client_id":"id","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","javascript_origins":["http://localhost:53404/"]}}
Now I can run this application but as mentioned getting error
Error: redirect_uri_mismatch
The redirect URI in the request: [[http://localhost:2430/authorize/]] did not match a registered redirect URI
I really don’t know from where this port number comes and from where this authorize comes. As you can see in my JSON, I have not set anything similar.
I am using VS 2010 SP1 Ultimate and Windows 7 home Basic.
I have tried to give complete information and my end goal is upload document in Google drive.

The tutorial you are following uses an older version of google.apis thats why you are seeing the not supported. Here are a couple of tutorials on how it works with the new version of the api.
http://daimto.com/google-oauth2-csharp/
http://daimto.com/google-drive-api-c/

Take a look in https://code.google.com/p/google-api-dotnet-client/source/browse/Tasks.ASP.NET.SimpleOAuth2/Default.aspx.cs?repo=samples. It's a Task API ASP.NET sample. As you can see you need to add several lines of code, but it works.

I downloaded the samples Eyal mentions and got that compiling before integrating with my own app. .Net needs to be 4 although it states it supports higher.

You have to set the redirect URI in the google developer console, not in your JSON. In your case, you would want to set it to http://localhost:2430/authorize/.

Related

Get role of current user in Sharepoint Online site using Microsoft Graph

I am building a .net core web app in which i use AzureAD auth and Microsoft Graph to get data from a sharepoint online site.
I need to get the groups of the current user.
I tried to use graphClient.Me.MemberOf.Request().GetAsync();
I think i'm getting the role of the user in the Azure directory.
But i want the role of the current user for a specific sharepoint online site.
Is that possible to get this information and how ?
I don't find a working way to get this using Microsoft Graph.
EDIT:
As Microsoft Graph doesn't allow to get that data.
I tried to call the following Sharepoint Online API endpoint :
https://{name}.sharepoint.com/sites/{name}/_api/web/currentUser?$select=Groups/Title&$expand=Groups
Using this api endpoint i can see all the roles of the current user in my browser.
But i don't find how to call it from my .net core web app.
Tried the following :
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Content-types", "application/json;odata=verbose");
var response = await client.GetAsync("https://{name}.sharepoint.com/sites/{name}/_api/web/currentUser?$select=Groups/Title&$expand=Groups");
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
}
But that give me a 403 response.
EDIT 2 :
I am currently trying to use CSOM to get this informations(TCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200.)
But i don't find a way to get TokenHelper.cs.
var token = TokenHelper.GetAppOnlyAccessToken(SharePointPrincipalId, webUri.Authority, null).AccessToken;
var ctx = TokenHelper.GetClientContextWithAccessToken(webUri.ToString(), token);
I tried to add "AppForSharePointOnlineWebToolkit" and it did not add the needed files in the project.
How can i get the TokenHelper.cs file ?
Thanks for any help.
Tristan
To execute CSOM code in .net core, do below settings.
We can install the package as below.
Install-Package TTCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200 -Version 16.1.8029.1200
More information is here: TTCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200
Or use the following solution from GitHub: NetCore.CSOM
Or follow the steps below.
1.Create a .NET Core console app.
2.Add the references: Microsoft.SharePoint.Client.Portable.dll, Microsoft.SharePoint.Client.Runtime.Portable.dll, and Microsoft.SharePoint.Client.Runtime.Windows.dll.
Note: If the project has references to Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll, please remove them.
These references can be accessed by installing CSOM library into another project, and then navigating to installed nuget packages in the file directory: c:\Users\user.nuget\packages\microsoft.sharepointonline.csom(version)\lib\netcore45
3.Add the code below to the .NET Core 2.0 console application:
Get current user role:
To get the current user role, you can use Web.GetUserEffectivePermissions method.
Ex:
ClientResult<BasePermissions> permission= web.GetUserEffectivePermissions(name);
context.ExecuteQuery();
var res = permission.Value;
Refer below link to get clientcontext using access token: https://www.sharepointpals.com/post/how-to-get-the-client-context-using-app-access-token-by-passing-client-id-and-client-secret-id-using-csom-in-sharepoint-office-365/
No. Microsoft Graph doesn't expose an endpoint that allows you to get the information of SharePoint Group and its members.
If you has this requirement, you could vote this idea on Microsoft Graph UserVoice.

Swagger w/ ASP.NET v5 Azure Api App

I'm attempting to set up a Api App (Azure) with Swagger + Swashbuckle as demonstrated by Scott Hanselman at the //Build conference here: http://channel9.msdn.com/Events/Build/2015/2-628.
I have installed (using NuGet) the packages Swagger.Api and Swashbuckle.Core. It hasn't added any controller or settings that I would expect in order to have a swagger page. When I navigate to {baseUrl}\swagger, I get a 404 error.
I would think that since it has a UI it would require a Web App in addition to the Api App, but I've rewatched the demo and Scott clearly says you can add Swagger + Swashbuckle to any Api App. In a 2nd app though I'd think there may be issues with Api discovery. Has anyone set this up yet successfully?
Time rolls on and now Swashbuckle works for vNext (beta8 for me, probably other versions too) - thank you to the team and contributors!
In project.json add the package:
"Swashbuckle": "6.0.0-*",
In startup.cs in ConfigureServices():
services.AddSwaggerGen();
services.ConfigureSwaggerDocument(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "My Super API",
Description = "A Super API using Swagger and Swashbuckle",
TermsOfService = ""
});
});
services.ConfigureSwaggerSchema(options =>{
options.DescribeAllEnumsAsStrings = true;
});
In startup.cs in Configure():
app.UseSwaggerGen();
app.UseSwaggerUi();
Now you can access your API doco - http://domain:port/swagger/ui/index.html
Access your Swagger definition - http://domain:port/swagger/v1/swagger.json
Then assuming you have at least one internet facing dev/uat/staging/prod environment, grab the definition URL then do File-> Import URI at http://editor.swagger.io/ - now you have code-gen for about 20 clients too :)
You can also setup your own code-gen server if you are so inclined too (https://github.com/swagger-api/swagger-codegen), however I leveraged the existing online generator. The online editor also has full model and relationship definitions too at least in my case where I fully defined my model using EF7 (I know, ick... but it's much better than <= EF6 and ServiceStack doesn't support CoreCLR, yet). Depending on the size of your project this could save you a few hours to a few weeks of work documenting, plus it is dynamically updating itself as you code more. And you can use it to test your endpoints too, but I still prefer PostMan.
Full sample project at https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample/tree/beta8
Big ups to MrSheepUK
HTH
This answer is now outdated. See the other answer.
There is a nice blogpost describing the problem you have: https://alexanderzeitler.com/articles/Deploying-a-ASP-NET-MVC-6-API-as-Azure-API-App-in-Azure-App-Services/
This describes how you can add the Ahoy! package to an ASP.NET v6 Web Api project and adding this as an API app to Azure.
Here is another source: http://devmeetsbi.ghost.io/help-and-test-page-for-asp-net-web-api-asp-net-5-and-mvc-6/
You did all the right steps, but unfortunately for ASP.NET 5, Swashbuckle doesn't work yet.
You can get Ahoy! which is the next version of Swashbuckle that has ASP.NET v6 support here. That should make everything work.

Unable to add reference to third party WCF Service using Visual Studio

I recently received an updated integration document from a third party web services provider. One of the changes indicated that there are updated services that are located at a new URL.
I proceeded to delete my existing reference in my .NET 3.5 console application (VS2012) and attempted to add a new one pointing to the updated URL but was unable to do so due to the following error and die disabled "Add" button:
I subsequently tried to restore the reference to the old URL (which worked a mere month ago when I initially added it) but that to gave the same error.
I did a bit of googling and found the following posts that were not very encouraging:
Can't add Service Reference
Mixing Add Service Reference and WCF Web HTTP (a.k.a. REST) endpoint does not work
Don’t use “Add Service Reference”!
The first link simply repeated my problem statement. The second suggested there is no hope for me and the third is of no relevance since I do not control the server side component.
I was hoping someone with some more WCF experience possibly point me in the right direction. I suppose my question is twofold:
1: Is this something I am doing wrong on my side or can it be a problem with configuration on the third party server?
2: Is there any way I can reasonably add a reference to those services in my project without using "Add Reference" in VS, using the WSDL which I am able to download?
Edit: Just to calrify, the URL resolves and I can see the service controls and descriptions in both my browser and the VS window where you attempt to resolve the WS but the ".../$metadata" part in the error message above is not part of the original URL I entered. original URL which resolves fine is http://service.somewhere/service234/thing and the error refers to http://service.somewhere/service234/thing/_vti_bin/ListData.svc/$metadata

Embedding a Flex Google Map in a WordPress Page

I'm embedding a Flex app in a WordPress page using Matt Carpenter's great plugin. It's a map created using Google Maps' Flash API.
I'm getting an initialization failure. Is that because the API key is in the Flex app and
Google can't "see" it?
This is the error that I'm getting:
Initialization failed: please check
the API key, swf location, version and
network availability.
Or do I have something else going on? Any suggestions?
Generally, the API key can only be used on 1 domain. Try creating a new key that's specifically to be used for online files. Use the other key you already have for offline testing.
Lewis

silverlight error - the opreation is not supported for a relative uri

I've just started developing silverlight applications. I've created a webserivce in my asp.net project. Now, when I try to connect to it through my silverlight project I receive the following error:
"the opreation is not supported for a relative uri"
I am using the following url -->
http://192.168.1.2/MyWebsite/SubVersionedHistory.svc
I can find the class and its methods, but I receive this horrific error when I add it.
Thank you for your help and advice,
Vondiplo
I don't think you're alone in hitting this problem. I hit it today with VS2008SP1 + SL2 trying to create a Service Reference for an ADO.NET Data Service. First time I've hit the error.
Others have detailed similar experiences to reach this error:
http://silverlight.net/forums/t/87535.aspx
http://silverlight.net/forums/t/56629.aspx
It's not entirely clear at this point if the issue is with the IDE "Add Service Reference" dialog or something specific in the services causing this error. In my case, however, my code still worked despite the error message. I simply passed the URL to my service in the constructor of my DataService proxy client, like this:
var context = new DataServiceContext(new Uri("NorthwindDataService.svc", UriKind.Relative));
Summary point: Just because you hit this error in the IDE, your service reference may still work. Give it a try and let us know if you're seeing errors at run time.
Hope that helps.
[UPDATE] Based on some other advice I've found and tested, you can also try:
deleting your Service Reference
deleting your ServiceReferences.ClientConfig file
saving your solution
and then closing and reopening it in VS.
The simple act of closing and reopening your project has been shown to fix several problems with the Add Service wizard. Re-run the Add Service Ref wizard and you may have better luck. I personally tested this solution on a project today and can confirm it works. Hope that adds extra help to finding your solution.
You should be more specific about your problem. For example, are you having this problem when adding the reference or when you actually try to consume the service?
It sounds like you need to be using a full path, including the "http://" but that is just a shot in the dark based on the error message you provide.
[edit]If you are using the built in ASP.NET server instead of IIS then be sure you set a specific port number and use it in your path. For example, I have used http://localhost:4940/MyService.svc for testing[/edit]
Maybe this response can help you
You cannot use AbsolutePath, You need to use AbsoluteURL. Build your URL this way:
Uri url = new Uri(App.Current.Host.Source, "../settings.xml");client.DownloadStringAsync(url);
http://silverlight.net/forums/p/28912/95541.aspx
HTH
Braulio
Check the ServiceReferences.ClientConfig
if there are multiple endpoints there you will get this exception. one thing that can cause this is referencing a service using casini, later switiching to IIS express and rereferencing the service.

Resources