I'm trying to create a wcf service that consumes Bing's Geocoding soap service. But when ever i try to set init a new GeoCodeRequest using it's constructor it returns a null.
When I call request.Query = address; I get a null value error referring to request .
public string RequestLocation(string address)
{
const string key = "mybingapplicationId";
var request = new GeocodeRequest();
request.Credentials.ApplicationId = key;
request.Query = address;
var filters = new ConfidenceFilter[1];
filters[0] = new ConfidenceFilter { MinimumConfidence = Confidence.High };
var geocodeOptions = new GeocodeOptions { Filters = filters };
request.Options = geocodeOptions;
// Make the geocode request
var geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
var geocodeResponse = geocodeService.Geocode(request);
return geocodeResponse.Results[0].DisplayName;
}
[UnitTest]
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Development\\WcfService1\\WcfService1", "/")]
[UrlToTest("http://localhost:24842/")]
[DeploymentItem("WcfService1.dll")]
public void RequestLocationTest()
{
var target = new TestService.BingEngineClient();
var address = string.Format("1600 Pennsylvania Avenue, {0}, {1}","Washington", "DC");
var expected = string.Empty;
var actual = target.RequestLocation(address);
Assert.IsNotNull(actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
First the code is missing initialization of the Credentials.
request.Credentials = new GeocodeService.Credentials();
When you go through Creating a Bing Maps Account you have to use their application to
Create a Bing Maps Key for the specific application in question. Note that this is different from your account key.
public string RequestLocation(string address)
{
var request = new GeocodeRequest {Credentials = new Credentials {ApplicationId = _key}, Query = address};
var filters = new ConfidenceFilter[1];
filters[0] = new ConfidenceFilter { MinimumConfidence = Confidence.High };
var geocodeOptions = new GeocodeOptions { Filters = filters };
request.Options = geocodeOptions;
// Make the geocode request
var geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
var geocodeResponse = geocodeService.Geocode(request);
return string.Format("Longitude:{0},Latitude:{1}", geocodeResponse.Results[0].Locations[0].Longitude,
geocodeResponse.Results[0].Locations[0].Latitude);
}
Related
I am writing a method to get DisplayName of off ObjectID. Is there a quick way to figure out if the ObjectID is a Group Or an User Or a ServicePrincipal
Below is my weird\Rough method which is working for me. But I wanted to check if anyone has any simpler Or cooler solution.
I tried searching online for some solutions but no luck yet.
public static async Task<string> GetDisplayName(string TenantID, string ObjectID, string MSGraphToken, string ObjectType)
{
string DisplayNameURI = null;
string DisplayName = null;
var DisplayNamehttpClient = new HttpClient
{
BaseAddress = new Uri("https://graph.windows.net/")
};
if (ObjectType.Equals("Decide", StringComparison.OrdinalIgnoreCase))
{
// trying servicePrincipals
ObjectType = "servicePrincipals";
DisplayNameURI = $"{TenantID}/{ObjectType}/{ObjectID}?api-version=1.6";
var SPNhttpClient = new HttpClient
{
BaseAddress = new Uri("https://graph.windows.net/")
};
SPNhttpClient.DefaultRequestHeaders.Remove("Authorization");
SPNhttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + MSGraphToken);
HttpResponseMessage SPNResponse = await SPNhttpClient.GetAsync(DisplayNameURI).ConfigureAwait(false);
var SPNHttpsResponse = await SPNResponse.Content.ReadAsStringAsync();
dynamic SPNResult = JsonConvert.DeserializeObject<object>(SPNHttpsResponse);
DisplayName = SPNResult.displayName;
if (string.IsNullOrEmpty(DisplayName) == true)
{
// Trying for Users
ObjectType = "users";
DisplayNameURI = $"{TenantID}/{ObjectType}/{ObjectID}?api-version=1.6";
var usershttpClient = new HttpClient
{
BaseAddress = new Uri("https://graph.windows.net/")
};
usershttpClient.DefaultRequestHeaders.Remove("Authorization");
usershttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + MSGraphToken);
HttpResponseMessage usersResponse = await usershttpClient.GetAsync(DisplayNameURI).ConfigureAwait(false);
var usersHttpsResponse = await usersResponse.Content.ReadAsStringAsync();
dynamic usersResult = JsonConvert.DeserializeObject<object>(usersHttpsResponse);
DisplayName = usersResult.displayName;
if (string.IsNullOrEmpty(DisplayName) == true)
{
//Trying for Groups
ObjectType = "groups";
DisplayNameURI = $"{TenantID}/{ObjectType}/{ObjectID}?api-version=1.6";
var groupshttpClient = new HttpClient
{
BaseAddress = new Uri("https://graph.windows.net/")
};
groupshttpClient.DefaultRequestHeaders.Remove("Authorization");
groupshttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + MSGraphToken);
HttpResponseMessage groupsResponse = await groupshttpClient.GetAsync(DisplayNameURI).ConfigureAwait(false);
var groupsHttpsResponse = await groupsResponse.Content.ReadAsStringAsync();
dynamic groupsResult = JsonConvert.DeserializeObject<object>(groupsHttpsResponse);
DisplayName = groupsResult.displayName;
}
}
}
else
{
DisplayNameURI = $"{TenantID}/{ObjectType}/{ObjectID}?api-version=1.6";
DisplayNamehttpClient.DefaultRequestHeaders.Remove("Authorization");
DisplayNamehttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + MSGraphToken);
HttpResponseMessage DisplayNameResponse = await DisplayNamehttpClient.GetAsync(DisplayNameURI).ConfigureAwait(false);
var DisplayNameHttpsResponse = await DisplayNameResponse.Content.ReadAsStringAsync();
dynamic DisplayNameResult = JsonConvert.DeserializeObject<object>(DisplayNameHttpsResponse);
DisplayName = DisplayNameResult.displayName;
}
//Console.WriteLine($"{DisplayName}");
if (string.IsNullOrEmpty(DisplayName) == true)
{
DisplayName = "Unknown";
}
return DisplayName;
}
You can directly find the AD object based on objectID using PowerShell.
Command: Get-AzureADObjectByObjectId -ObjectIds objectID1,objected2
Reference: Get-AzureADObjectByObjectId (AzureAD) | Microsoft Docs
Otherwise you can using C# for calling graph api to get the details.
Code:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var ids = new List<String>()
{
"objectID1",
"objectID2"
};
var types = new List<String>()
{
"user"
};
await graphClient.DirectoryObjects
.GetByIds(ids,types)
.Request()
.PostAsync();
Reference: directoryObject: getByIds - Microsoft Graph v1.0 | Microsoft Docs
I am new to REST API JSON but have been able to consume a few API with asp.net. Here is the problem I am currently facing.
I have been able to send JSON data to an API using this method.
public void PostData()
{
string sName = sysName.Text;
string sDescrip = sysDescrip.Text;
var httpclient = new HttpClient();
httpclient.BaseAddress = new Uri("http://33.248.292.99:8094/bizzdesk/sysconfig/api/");
var sys = new Bizsys(){name= sName, description= sDescrip};
httpclient.PostAsJsonAsync("system", sys);
}
it work just fine.
Now I modify the code in order to accommodate more values thus:
var httpclient = new HttpClient();
// ArrayList paramList = new ArrayList();
httpclient.BaseAddress = new Uri("http://179.683.197.115:9091/tua-slte/api/organisations/");
var org = new Organisation() { id=1, name = "inno", abbreviation = "abx", type="school", sort = 7, isShow = true, createdBy=8, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017"};
var bas = new Basic() { id=1, username = "inno", password = "123", firstName="Innocent", lastName="Ujata", email = "ea#bizz.co", mobile = "123456", photo="10201001", loginIp="127.0.0.1", loginDate="10/04/2017", locked=false, organisation = org, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017", admin=true};
var org2 = new Organisation2() { id=1, name = "inno", abbreviation = "abx", type="school", sort = 7, isShow = true, createdBy=17, createdDate = "10/04/2017", editedBy = 09, editDate="11/04/2017"};
var hq = new HeadQuarter() { zonId=09, organisation = org2, zonCode = "123", zonName = "Abuja", zonAddress = "123456", zonCity = "Abuja", zonPostalCode = "120076", zonEmail = "answers", zonPhoneNumber = "0908765", zonFaxNumber = "1212", zonState = "FCT", createdBy=17, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017", version=1};
var examp = new RootObject() {basic=bas, headQuarter=hq };
var status = httpclient.PostAsJsonAsync("register", examp);
return status;
It keep returning this:
Id = 19, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
I hard coded the data to see it work first before making it dynamic.
I have tried using await async method too the result is the same.
all the questions and answers I have seen here are not similar.
What does that mean, and what am I getting wrong?
You are calling an async method, but not awaiting it's results.
Change:
var status = httpclient.PostAsJsonAsync("register", examp);
to
var status = await httpclient.PostAsJsonAsync("register", examp);
Extension method PostAsJsonAsync returns Task<HttpResponseMessage>. You grab this task and see it's details. If you want to return status code of completed request, you should await for task completion and then grab status code from response message:
private async Task<HttpStatusCode> PostSomethingAsync()
{
...
var response = await httpclient.PostAsJsonAsync("register", examp);
return response.StatusCode;
}
Or you can synchronously wait for request completion:
private HttpStatusCode PostSomething()
{
...
var response = httpclient.PostAsJsonAsync("register", examp).Result;
return response.StatusCode;
}
That's cause you are not awaiting the call like
var status = await httpclient.PostAsJsonAsync("register", examp);
Or, use ConfigureAwait()
var status = httpclient.PostAsJsonAsync("register", examp).ConfigureAwait(false);
I'm creating an app that will create a section associated the orgunitid created by the user. I'm using c#.
However, the code below does not work. When I debugged it, the number of attempts is 0. What should I do to send a request and create a new section properly?
private void ApiCreateSection(string host, ID2LUserContext userContext)
{
string orgUnitId = textBoxOrgUnitId.Text;
string sectionCreateRoute = "/d2l/api/lp/1.0/" + orgUnitId + "/sections/";
var client = new RestClient(host);
var valenceAuthenticator = new D2L.Extensibility.AuthSdk.Restsharp.ValenceAuthenticator(userContext);
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
requestCreateSection.AddJsonBody(new
{
Name = "Section Test1",
Code = "123123",
Description = new { Content = "Description", Type = "Html" }
});
valenceAuthenticator.Authenticate(client, requestCreateSection);
}
Assume I already have purchased a domain example.com with IP address 203.0.113.2. Using C# and the The Amazon Web Services SDK for .NET 2.0.2.2, I'd like to create a static website using a custom domain using Amazon S3 and Route 53. The manual process is described in the Amazon documentation.
When trying to create an alias, I get an exception with the message:
Invalid XML ; cvc-complex-type.2.4.a: Invalid content was found starting with element 'AliasTarget'.
One of '{"https://route53.amazonaws.com/doc/2012-12-12/":ResourceRecords}' is expected.
First, I created or updated a bucket (e.g. "example.com") in Amazon S3. If it already existed, content is deleted.
using (var client = AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.USWest1))
{
if (!S3BucketExists(name, client))
{
client.PutBucket(new PutBucketRequest
{
BucketName = name,
BucketRegion = S3Region.USW1,
CannedACL = S3CannedACL.PublicRead
});
}
else
{
var request = new ListObjectsRequest
{
BucketName = name
};
var objects = client.ListObjects(request).S3Objects;
foreach (var o in objects)
{
client.DeleteObject(new DeleteObjectRequest
{
BucketName = name,
Key = o.Key
});
}
client.PutACL(new PutACLRequest
{
CannedACL = S3CannedACL.PublicRead,
BucketName = name
});
}
client.PutBucketWebsite(new PutBucketWebsiteRequest
{
BucketName = name,
WebsiteConfiguration = new WebsiteConfiguration
{
ErrorDocument = "404.html",
IndexDocumentSuffix = "index.html"
}
});
CreateObject(name, client, "index.html", "text/html", "<p>The site is under maintenance</p>");
CreateObject(name, client, "404.html", "text/html", "<p>Not Found</p>");
}
S3BucketExists returns whether a bucket exist or not, and CreateObject creates a simple page and uploads it to the bucket. Its omitted for brevity sake. I'm able to connect to the S3 hosted site without any problems.
Then I use the Route 53 API to update an existing hosted zone or create one for "example.com". All resources, except for the SOA and NS entries are deleted.
using (var client = AWSClientFactory.CreateAmazonRoute53Client())
{
var hostedZone = FindHostedZoneByName(client, domainName);
if (hostedZone != null)
{
var resourceRecordSets = client.ListResourceRecordSets(new ListResourceRecordSetsRequest
{
HostedZoneId = hostedZone.Id,
});
bool hasElements = false;
var request1 = new ChangeResourceRecordSetsRequest
{
HostedZoneId = hostedZone.Id,
ChangeBatch = new ChangeBatch
{
Changes = new List<Change>()
}
};
foreach (var resourceRecordSet in resourceRecordSets.ResourceRecordSets)
{
switch (resourceRecordSet.Type)
{
case "SOA":
case "NS":
continue;
}
var change = new Change
{
Action = "DELETE",
ResourceRecordSet = resourceRecordSet
};
request1.ChangeBatch.Changes.Add(change);
hasElements = true;
}
if (hasElements)
{
var response = client.ChangeResourceRecordSets(request1);
}
}
else
{
hostedZone = CreateHostedZone(client, domainName);
}
var hostedZoneId = hostedZone.Id;
var request = new ChangeResourceRecordSetsRequest
{
HostedZoneId = hostedZoneId,
ChangeBatch = new ChangeBatch
{
Changes = new List<Change>
{
new Change
{
Action = ChangeAction.CREATE,
ResourceRecordSet = new ResourceRecordSet
{
Name = GetQualifiedName(domainName),
Type = RRType.A,
TTL = 300,
AliasTarget = new AliasTarget()
{
HostedZoneId = "Z2F56UZL2M1ACD",
DNSName = "s3-website-us-west-1.amazonaws.com.",
},
},
},
}
}
};
client.ChangeResourceRecordSets(request);
}
The hosted zone id ("Z2F56UZL2M1ACD") and DNS names ("s3-website-us-west-1.amazonaws.com.") are public knowledge and documented on Amazon's website.
The call to ChangeResourceRecordSets throws the exception. I created an empty ResourceRecords list, with a A record of "203.0.113.2", but have not had any luck creating an alias.
That said, I can manually create the alias to the Amazon S3 site afterwards using the "Route 53 Management Console". I'm sure it's something small I'm missing.
After re-reading the documentation, it turns out that one cannot specify the TTL when specifying an alias. The following change works. Replace the code that creates an instance of ChangeResourceRecordSetsRequest to the following:
var request = new ChangeResourceRecordSetsRequest
{
HostedZoneId = hostedZoneId,
ChangeBatch = new ChangeBatch
{
Changes = new List<Change>
{
new Change
{
Action = ChangeAction.CREATE,
ResourceRecordSet = new ResourceRecordSet
{
Name = GetQualifiedName(domainName),
Type = RRType.A,
AliasTarget = new AliasTarget
{
HostedZoneId = "Z2F56UZL2M1ACD",
DNSName = "s3-website-us-west-1.amazonaws.com.",
EvaluateTargetHealth = false,
},
},
},
}
}
};
The difference was evident when the output produced by System.Net tracing was compared to the request specified in the Amazon example.
Working Platform: ASP.NET 4.0 C# ( Framework Agnostic )
Google GData is my dependency
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Documents;
I have two pages Auth and List.
Auth redirects to Google Server like this
public ActionResult Auth()
{
var target = Request.Url.ToString().ToLowerInvariant().Replace("auth", "list");
var scope = "https://docs.google.com/feeds/";
bool secure = false, session = true;
var authSubUrl = AuthSubUtil.getRequestUrl(target, scope, secure, session);
return new RedirectResult(authSubUrl);
}
Now it reaches the List Page if Authentication is successful.
public ActionResult List()
{
if (Request.QueryString["token"] != null)
{
String singleUseToken = Request.QueryString["token"];
string consumerKey = "www.blahblah.net";
string consumerSecret = "my_key";
string sessionToken = AuthSubUtil.exchangeForSessionToken(singleUseToken, null).ToString();
var authFactory = new GOAuthRequestFactory("writely", "qwd-asd-01");
authFactory.Token = sessionToken;
authFactory.ConsumerKey = consumerKey;
authFactory.ConsumerSecret = consumerSecret;
//authFactory.TokenSecret = "";
try
{
var service = new DocumentsService(authFactory.ApplicationName) { RequestFactory = authFactory };
var query = new DocumentsListQuery();
query.Title = "project";
var feed = service.Query(query);
var result = feed.Entries.ToList().ConvertAll(a => a.Title.Text);
return View(result);
}
catch (GDataRequestException gdre)
{
throw;
}
}
}
This fails at the line var feed = service.Query(query); with the error
Execution of request failed: https://docs.google.com/feeds/default/private/full?title=project
The HttpStatusCode recieved on the catch block is HttpStatusCode.Unauthorized
What is wrong with this code? Do I need to get TokenSecret? If so how?
You need to request a token from Google and use it to intialize your DocumentsService instance.
Here's an example using Google's ContactsService. It should be the same for the DocumentsService.
Service service = new ContactsService("My Contacts Application");
service.setUserCredentials("your_email_address_here#gmail.com", "yourpassword");
var token = service.QueryClientLoginToken();
service.SetAuthenticationToken(token);
But as you mentioned, you are using AuthSub. I jumped the gun a bit too fast.
I see that you are requesting a session token. According to the documentation of the API you must use the session token to authenticate requests to the service by placing the token in the Authorization header. After you've set the session token, you can use the Google Data APIs client library.
Here's a complete example (by Google) on how to use AuthSub with the .NET client library:
http://code.google.com/intl/nl-NL/apis/gdata/articles/authsub_dotnet.html
Let me include a shortened example:
GAuthSubRequestFactory authFactory =
new GAuthSubRequestFactory("cl", "TesterApp");
authFactory.Token = (String) Session["token"];
CalendarService service = new CalendarService(authFactory.ApplicationName);
service.RequestFactory = authFactory;
EventQuery query = new EventQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/default/private/full");
EventFeed calFeed = service.Query(query);
foreach (Google.GData.Calendar.EventEntry entry in calFeed.Entries)
{
//...
}
And if I see correctly your example code pretty follows the same steps, except that you set the ConsumerKey and ConsumerSecret for the AuthFactory which is not done in the example by Google.
Used the 3-legged OAuth in the Google Data Protocol Client Libraries
Sample Code
string CONSUMER_KEY = "www.bherila.net";
string CONSUMER_SECRET = "RpKF7ykWt8C6At74TR4_wyIb";
string APPLICATION_NAME = "bwh-wssearch-01";
string SCOPE = "https://docs.google.com/feeds/";
public ActionResult Auth()
{
string callbackURL = String.Format("{0}{1}", Request.Url.ToString(), "List");
OAuthParameters parameters = new OAuthParameters()
{
ConsumerKey = CONSUMER_KEY,
ConsumerSecret = CONSUMER_SECRET,
Scope = SCOPE,
Callback = callbackURL,
SignatureMethod = "HMAC-SHA1"
};
OAuthUtil.GetUnauthorizedRequestToken(parameters);
string authorizationUrl = OAuthUtil.CreateUserAuthorizationUrl(parameters);
Session["parameters"] = parameters;
ViewBag.AuthUrl = authorizationUrl;
return View();
}
public ActionResult List()
{
if (Session["parameters"] != null)
{
OAuthParameters parameters = Session["parameters"] as OAuthParameters;
OAuthUtil.UpdateOAuthParametersFromCallback(Request.Url.Query, parameters);
try
{
OAuthUtil.GetAccessToken(parameters);
GOAuthRequestFactory authFactory = new GOAuthRequestFactory("writely", APPLICATION_NAME, parameters);
var service = new DocumentsService(authFactory.ApplicationName);
service.RequestFactory = authFactory;
var query = new DocumentsListQuery();
//query.Title = "recipe";
var feed = service.Query(query);
var docs = new List<string>();
foreach (DocumentEntry entry in feed.Entries)
{
docs.Add(entry.Title.Text);
}
//var result = feed.Entries.ToList().ConvertAll(a => a.Title.Text);
return View(docs);
}
catch (GDataRequestException gdre)
{
HttpWebResponse response = (HttpWebResponse)gdre.Response;
//bad auth token, clear session and refresh the page
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Session.Clear();
Response.Write(gdre.Message);
}
else
{
Response.Write("Error processing request: " + gdre.ToString());
}
throw;
}
}
else
{
return RedirectToAction("Index");
}
}
This 2-legged sample never worked for me for google docs.