예제 #1
0
      public static async Task<string> GetDrivesApiCallForSelectedFile( this SharePointOnlineUri fileHandlerFileGetOrPutUri, Guid FileHandlerFileId, AuthenticationResult SharepointAdalAuthResult )
      {
         using ( var context = new ClientContext( fileHandlerFileGetOrPutUri.GetSharePointWebUrlFromFileHandlerGetPutUri() ) )
         {
            context.ExecutingWebRequest += ( sender, e ) =>
                                       {
                                          e.WebRequestExecutor.RequestHeaders.Add( "Authorization", SharepointAdalAuthResult.CreateAuthorizationHeader() );
                                       };

            var webRequestUrl = await Task.Run( () =>
                                       {
                                          return GetSharePointMetaData( context, FileHandlerFileId );
                                       } );

            JObject oneDriveResult = await Task.Run( async () =>  
                                       {
                                          string result = string.Empty;
                                          using ( var request = new WebClient() )
                                          {
                                             request.Headers.Add( "Authorization", SharepointAdalAuthResult.CreateAuthorizationHeader() );
                                             result = await request.DownloadStringTaskAsync( webRequestUrl );
                                          }
                                          return JObject.Parse( result );
                                       } );

            var driveId = oneDriveResult["parentReference"]["driveId"].ToString();
            var itemId = oneDriveResult["id"].ToString();

            return $"drives/{driveId}/items/{itemId}";

         }
      }
예제 #2
0
      internal static JObject JSONResponse( string url, HttpMethod method, AuthenticationResult authResult, string apiPayload = "" )
      {
         HttpWebRequest webClientRequest;
         webClientRequest = WebRequest.CreateHttp( new Uri( url ) );
         webClientRequest.Method = method.ToString();
         webClientRequest.Headers.Add( "Authorization", authResult.CreateAuthorizationHeader() );
         if ( method != HttpMethod.Get )
         {
            using ( var writer = new StreamWriter( webClientRequest.GetRequestStream() ) )
            {
               writer.Write( apiPayload );

            }
         }
         WebResponse response;
         JObject jsonResponse = null;
         try
         {
            response = webClientRequest.GetResponse();
         }
         catch ( WebException webEx )
         {
            response = webEx.Response;
         }
         using ( var reader = new StreamReader( response.GetResponseStream() ) )
         {
            jsonResponse = JObject.Parse( reader.ReadToEnd() );
         }
         return jsonResponse;
      }
        //Annotate Data Asset:
        // The Annotate Data Asset operation annotates an asset.
        static string AnnotateDataAsset(string catalogName, string viewType, string jsonNode, string json)
        {
            string responseString = string.Empty;

            //Get access token to use to call operation
            authResult = AccessToken();

            string fullUri = string.Format("https://{0}.datacatalog.azure.com/{1}/views/{2}/{3}?api-version=2015-07.1.0-Preview",
                authResult.TenantId, catalogName, viewType, jsonNode);

            //Create a POST WebRequest as a Json content type
            HttpWebRequest request = System.Net.WebRequest.Create(fullUri) as System.Net.HttpWebRequest;
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json";

            //To authorize the operation call, you need an access token which is part of the Authorization header
            request.Headers.Add("Authorization", authResult.CreateAuthorizationHeader());

            //Add a guid to help with diagnostics
            string guid = Guid.NewGuid().ToString();
            request.Headers.Add("x-ms-client-request-id", guid);

            //POST web request
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);
            request.ContentLength = byteArray.Length;

            //Write JSON byte[] into a Stream and get web response
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(byteArray, 0, byteArray.Length);

                try
                {
                    using (var httpWebResponse = (HttpWebResponse)retryPolicy.ExecuteAction(() => (request.GetResponse())))
                    {
                        StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream());
                        responseString = reader.ReadToEnd();
                    }
                }
                catch (WebException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.Status);
                    if (ex.Response != null)
                    {
                        // can use ex.Response.Status, .StatusDescription
                        if (ex.Response.ContentLength != 0)
                        {
                            using (var stream = ex.Response.GetResponseStream())
                            {
                                using (var reader = new StreamReader(stream))
                                {
                                    Console.WriteLine(reader.ReadToEnd());
                                }
                            }
                        }
                    }
                    responseString = null;
                }
            }

            return responseString;
        }