Zied.M Zied.M -3 years ago 104
Vb.net Question

Share a document with specified user in Google Drive

I want to share a file that exists in Google Drive with a specified user. The file is not public.

Firstly, I want to show a file in a WebBrowser control.
Secondly, I want that when I share an URL to a different user, they can then show the file in their Google Drive.

Here is my code, I already can do the first step but for the second step, I don't know how I can do it.

Public Sub A(fichier As String)
CreateService()
Dim url As String = ""
Dim list = Service.Files.List()
Dim count = list.Execute()
For Each fich In count.Items
If (fich.Title) = fichier Then
url = fich.WebContentLink
Exit For
End If
Next
affich_image.img.Navigate(url)
affich_image.Show()

Answer Source

You can use the permissions.insert method.

Inserts a permission for a file or Team Drive.

To see the parameters and further details, try to visit the documentation.

Here, I provide you the sample code for .NET that was introduced in the said documentation.

using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;

using System.Net;
// ...

public class MyClass {

  // ...

  /// <summary>
  /// Insert a new permission.
  /// </summary>
  /// <param name="service">Drive API service instance.</param>
  /// <param name="fileId">ID of the file to insert permission for.</param>
  /// <param name="value">
  /// User or group e-mail address, domain name or null for "default" type.
  /// </param>
  /// <param name="type">The value "user", "group", "domain" or "default".</param>
  /// <param name="role">The value "owner", "writer" or "reader".</param>
  /// <returns>The inserted permission, null is returned if an API error occurred</returns>
  public static Permission InsertPermission(DriveService service, String fileId, String value,
      String type, String role) {
    Permission newPermission = new Permission();
    newPermission.Value = value;
    newPermission.Type = type;
    newPermission.Role = role;
    try {
      return service.Permissions.Insert(newPermission, fileId).Execute();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
    return null;
  }

  // ...

}

You may also visit this SO post for further discussion.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download