Google Custom Search .NET API Compile issue with Fetch method - c#

I am attempting to use the Google CustomSearch API for .NET using sample code from the following locations:
Steps for using Google custom search API in .NET
How can I do a search with Google Custom Search API for .NET?
http://astrocoder.com/search-using-google-custom-search/
The basic code in it's simplest form looks like:
string apiKey = "my-api-key";
string cseKey = "my-cse-id";
string query = "search query";
var bcsi = new BaseClientService.Initializer { ApiKey = apiKey };
var css = new CustomsearchService(bcsi);
var listRequest = css.Cse.List(query);
listRequest.Cx = cseKey;
Search search = listRequest.Fetch();
However, when I attempt to compile this, I am getting the following error:
'Google.Apis.Customsearch.v1.CseResource.ListRequest' does not
contain a definition for 'Fetch' and no extension method 'Fetch'
accepting a first argument of type
'Google.Apis.Customsearch.v1.CseResource.ListRequest' could be found
As far as I can tell, I have all of the required library files (I used NuGet to install the Google APIs). When I view the API documentation, I do not see a Fetch() method, however, all of the sample code I have been able to find shows the listRequest.Fetch() method call.
https://developers.google.com/resources/api-libraries/documentation/customsearch/v1/csharp/latest/classGoogle_1_1Apis_1_1Customsearch_1_1v1_1_1CseResource_1_1ListRequest-members.html

Instead of using Fetch() you can use the following.
Search search = listRequest.Execute();
The fetch() api has been replaced after version 1.4 but the sample code has not been updated yet.

Related

C# SP-API giving error of signature for getReportDocument but it works fine for other methods

I am using C# Amazon SP-API library, I have converted C# wrapper classes using swagger codegen and all working fine with Pricing and also some of function in Reports API.
But when I tried to call getReportDocument, it gives error of AWS Signature does not match.
https://github.com/amzn/selling-partner-api-docs/blob/main/references/reports-api/reports_2020-09-04.md#getreportdocument
If you can confirm that the problem occurs only when calling single items by id like GetOrder, GetReport and you are using Amazon.SellingPartnerAPIAA.csproj provided by amazon for signing, it is most likely that AWSSigV4Signer.CreateCanonicalRequest Method signs the url with the segment placeholder not the id itself.
e.g. "/reports/2021-06-30/documents/{reportDocumentId}" instead of "/reports/2021-06-30/documents/0356cf79-b8b0-4226-b4b9-0ee058ea5760"
One possible solution is resolving the url "by hand" before calling ExtractCanonicalURIParameters
foreach(var par in restRequest.Parameters)
{
if(restRequest.Resource.Contains($"{{{par.Name}}}"))
{
restRequest.Resource = restRequest.Resource.Replace($"{{{par.Name}}}", par.Value.ToString());
}
}
//CanonicalURI
canonicalizedRequest.AppendFormat("{0}\n", AwsSignerHelper.ExtractCanonicalURIParameters(restRequest.Resource));

VersionOne: Getting message that VersionOneAPIConnector is obsolete and I should use V1Connector instead. But how?

I am trying to upgrade my c# programs to VersionOne.SDK.API version 15.3.0.0. I am getting a message that VersionOneAPIConnector is obsolete and that I should use V1Connector instead. When the ObjectModel API was discontinued, I had changed to doing everything with VersionOneAPIConnector using query.v1 (using HttpPost) and rest-1.v1/Data(using SendData). After getting the new version, I figured out how to use V1Connector and pass it to Services and then to use ExecutePassThroughQuery() to pass what had been the 2nd argument to HttpPost for my query.v1 code like this:
Original code:
Stream resultStream = _cx.HttpPost("/query.v1", System.Text.Encoding.UTF8.GetBytes(GetQueryForMembers()));
using (StreamReader reader = new StreamReader(resultStream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
New Code:
result = _service.ExecutePassThroughQuery(GetQueryForMembers());
Where GetQueryforMembers() is:
public static string GetQueryForMembers()
{
return #"
from: Member
select:
- Email
where:
AssetState: Active";
}
But, I can't figure out how to use V1Connector/IServices for my rest/SendData code. All the Rest examples on the VersionOne site now show using it from the API Console, not from stand-alone code. It looks like at least some of the things I was doing with Rest can be done via Services.executeOperation() or Services.Meta, but the V1 employee that I was working with to get rid of my Object Model code a few years ago had recommended only using the API for the connection and to otherwise use Rest and Query, so that I didn't have to worry about compatibility with .NET versions. But, from looking at the examples currently provided and the functionality available on the V1Connector/IServices classes, it looks like maybe V1 wants us to use Meta API rather than REST now? Or is there a way to use REST directly that I am missing? From looking at the source, I see there is an internal UseDataAPI() on the V1Connector that IServices uses. But, it only uses it in Save, ExecuteOperation and Retrieve and it doesn't look like any of those do what I'm trying to do.
This is an example, if what I was doing before with REST (where _passed_cx is a VersionOneAPIConnector)
string newDefect = string.Concat("<Asset href=\"/MentorG01/rest-1.v1/New/Defect\">",
"<Attribute name=\"Name\" act=\"set\">", cdata_attribute_value, "</Attribute>",
"<Relation name=\"Scope\" act=\"set\">",
"<Asset href=\"/MentorG01/rest-1.v1/Data/Scope/", project_oid.Split(':')[1], "\" idref=\"", project_oid, "\" />",
"</Relation>",
"</Asset>");
string url = "rest-1.v1/Data/Defect";
Stream resultStream = _passed_cx.SendData(url, newDefect);
Anyone know how to use rest-1.v1 with V1Connector? If so, can you provide an example?

Support Token Based Authentication in Swagger Documentation for Web API

I am trying out swagger(SwashBuckle) for generating web api documentation. I have successfully generated the documentation using Web API documentation using swagger but I am not able to successfully send API requests, as we have token based authentication (custom header for authentication purpose) for few of the methods.
I tried to find out sample code/resources for the same but did not have much luck. Please let me know if someone has implemented/came across similar thing in their application.
I had the same problem some time ago, and asked in the blog http://bitoftech.net/2014/08/25/asp-net-web-api-documentation-using-swagger/ for a solution.
This was the answer that worked for me:
1.Add new file named “SwaggerExtensions”, then added new JS file named “onComplete.js”, you have to change the build action for this file to “Embedded Resource”.
2.Inside the file “onComplete.js” paste the following code:
$('#input_apiKey').change(function () {
var key = $('#input_apiKey')[0].value;
if (key && key.trim() != "") {
key = "Bearer " + key;
window.authorizations.add("key", new ApiKeyAuthorization("Authorization", key, "header"));
}
});
3.Open file “SwaggerConfig.cs” and inside the register method paste the code below:
SwaggerUiConfig.Customize(c =>
{
c.SupportHeaderParams = true;
c.InjectJavaScript(typeof(SwaggerConfig).Assembly, "AngularJSAuthentication.API.SwaggerExtensions.onComplete.js");
});
Note that you need to change the full assembly name to match your assembly name.
I believe thats it, once you run the UI you will notice that this file has been downloaded and it will set the authorization header correctly.
You need to set up API Key authorisation. The "Describing Security/Authorization Schemes" in the README at https://github.com/domaindrivendev/Swashbuckle has details on this but in short you need to do something like the following in your call to httpConfiguration.EnableSwagger()
c.ApiKey("apiKey")
.Description("API Key Authentication")
.Name("apiKey")
.In("header");
You then need to create a custom attribute derived from IDocumentFilter and apply it to the appropriate methods in your controllers. Lets say you call this ApiKeyFilter. You then need to register this with Swagger by adding the following in EnableSwagger()
c.OperationFilter<ApiKeyFilter>();
I'm not aware of sample code for an ApiKey attribute but I've used https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Dummy.Core/SwaggerExtensions/AssignOAuth2SecurityRequirements.cs before for OAuth2, you should be able to adapt that.

.NET Toolkit for Rally REST API Documentation deprecated?

I'm starting to work with Rally .NET API, in order to develop a plugin to import User Story into Enterprise Architect.
I have started by the examples in the following page: http://developer.help.rallydev.com/rest-api-net.
In the last one, for example, I got this errors:
//Create an item
DynamicJsonObject toCreate = new DynamicJsonObject();
toCreate["Name"] = "My Defect";
CreateResult createResult = restApi.Create("defect", toCreate);
Error 1 No overload for method 'Create' takes 2 arguments
//Delete the item
OperationResult deleteResult = restApi.Delete(createResult.Reference);
Error 2 No overload for method 'Delete' takes 1 arguments
But the documentation here is different from the examples.
All in all, I would like to know any good sources of information to learn this Rally API and which is the correct implementation for the Create and Delete in the examples of the first page.
Thanks in advance,
Pedro
Sorry for the confusion - with the latest release of the .NET REST DLL (version 1.0.15), both the Create and Delete methods changed slightly - they now require a Workspace Ref:
String workspaceRef = "/workspace/12345678910";
DynamicJsonObject toCreate = new DynamicJsonObject();
toCreate["Name"] = "My Defect";
CreateResult createResult = restApi.Create(workspaceRef, "defect", toCreate);
Delete should look like this:
OperationResult deleteResult = restApi.Delete(workspaceRef, createResult.Reference);
Or this:
myDefectObjectID = "12345678911";
OperationResult deleteResult = restApi.Delete(workspaceRef, "Defect", myDefectObjectID);
We'll work to get the documentation updated as quickly as possible. Thanks for pointing this out!

c# program to call Google+ API and Custom Search API from Google

Can you please tell me via a code sample how I can write a C# program that will call
the Google+ API and Custom Search API of Google.
I know there is a brief description of it on :::::
URL:https://developers.google.com/+/api/
URL:https://developers.google.com/custom-search/v1/using_rest
URL:http://code.google.com/p/google-api-dotnet-client/wiki/OAuth2
But the process mentioned in the links in doing the above as given in the documentation is
not very clear.
Is there a simple illustration through C# code that I can use to call Google+ API and Custom
Search API?
The Google API Dotnet Client project contains .NET wrappers form most of Google's APIs, including Google+.
They do have several examples but not for all of their APIs, yet.
A Google+ example from their page to query for public activities is:
CommandLine.EnableExceptionHandling();
CommandLine.DisplayGoogleSampleHeader("PlusService API");
// Create the service.
var objService= new PlusService();
objService.Key = ""; //put in user key.
var acts = objService.Activities.Search();
acts.Query = "super cool";
acts.MaxResults = 10;
var searchResults = acts.Fetch();
if (searchResults.Items != null)
{
foreach (Activity feed in searchResults.Items)
{
//extract any property of uer interest
CommandLine.WriteResult(feed.Title, feed.Actor);
}
}

Categories