Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Problem to Accessing Remote Data

$
0
0

Hello everyone my name is Taniguchi and i have this code to connect to dynamics 365 enviromment
public MainPage()
{
InitializeComponent();
}
private void PressMeButton_Clicked(object sender, EventArgs e)
{
(sender as Button).Text = "I was just clicked!";
var contacts = CrmRequest(
HttpMethod.Get,
"https://ax4bdev.api.crm2.dynamics.com/api/data/v9.1/contacts")
.Result.Content.ReadAsStringAsync();
}

    public async Task<String>  AccessTokenGenerator()
    {
        string redirectUrl = "https://ax4bdev.api.crm2.dynamics.com/api/data/v9.1/";//url da api da instancia do dynamics
        string clientId = "2b121ed5-9fe6-4ddf-bdea-9bbe8cd37bd0";
        string clientSecret = "_BMqI@uk2tq_@aKKI17rn3M8WzWTeehZ";
        string authority = "https://login.microsoftonline.com/21c3dfd6-b6f1-4b0a-87e8-c4b017582110";
        string resourceUrl = "https://ax4bdev.crm2.dynamics.com/";
        ClientCredential credentials = new ClientCredential(clientId, clientSecret);
        var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
        var result = await authContext.AcquireTokenAsync(resourceUrl, credentials);
        return result.AccessToken;
    }
    public async Task<HttpResponseMessage> CrmRequest(HttpMethod httpMethod, string requestUri, string body = null)
    {
        // Acquiring Access Token  
        var accessToken = await AccessTokenGenerator();

        var client = new HttpClient();
        var message = new HttpRequestMessage(httpMethod, requestUri);

        // OData related headers  
        message.Headers.Add("OData-MaxVersion", "4.0");
        message.Headers.Add("OData-Version", "4.0");
        message.Headers.Add("Prefer", "odata.include-annotations=\"*\"");

        // Passing AccessToken in Authentication header  
        message.Headers.Add("Authorization", $"Bearer {accessToken}");

        // Adding body content in HTTP request   
        if (body != null)
            message.Content = new StringContent(body, UnicodeEncoding.UTF8, "application/json");

        return await client.SendAsync(message);
    }

but on my android emulator it doenst appear the screen for to log into dynamics enviroments intead dont happen nothing, doenst appear any error messages when i try to debug when a pass this line var result = await authContext.AcquireTokenAsync(resourceUrl, credentials); dont happens nothing next it like the code frozed.
is there somenthing to actvite to appear the log into screen or the code is placed somethere else ?
thanks for help


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>