how to post images using plusdoamins on google plus

768 views Asked by At

I want to post/upload an image/picture to Google+ stream/wall in C# asp.net I have Google a lot but could not fine any solution so that i could post an image from my application to Google plus wall. please help. Thanks in advance

1

There are 1 answers

10
Linda Lawton - DaImTo On

I don't have access to a Google+ domain account so cant help you test this. However I have a generated sample that might get you started

Oauth2

  /// <summary>
        /// Authenticate to Google Using Oauth2
        /// Documentation https://developers.google.com/accounts/docs/OAuth2
        /// </summary>
        /// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
        /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
        /// <param name="userName">The user to authorize.</param>
        /// <returns>a valid PlusDomainsService</returns>
        public static PlusDomainsService AuthenticateOauth(string clientId, string clientSecret, string userName)
        {
            if (string.IsNullOrEmpty(clientId))
                throw new Exception("clientId is required.");
            if (string.IsNullOrEmpty(clientSecret))
                throw new Exception("clientSecret is required.");
            if (string.IsNullOrEmpty(userName))
                throw new Exception("userName is required for datastore.");


            string[] scopes = new string[] { PlusDomainsService.Scope.PlusCirclesRead,     // View your circles and the people and pages in them
                                             PlusDomainsService.Scope.PlusCirclesWrite,    // Manage your circles and add people and pages. People and pages you add to your circles will be notified. Others may see this information publicly. People you add to circles can use Hangouts with you.
                                             PlusDomainsService.Scope.PlusLogin,           // Know your basic profile info and list of people in your circles.
                                             PlusDomainsService.Scope.PlusMe,              // Know who you are on Google
                                             PlusDomainsService.Scope.PlusMediaUpload,     // Send your photos and videos to Google+
                                             PlusDomainsService.Scope.PlusProfilesRead,    // View your own Google+ profile and profiles visible to you
                                             PlusDomainsService.Scope.PlusStreamRead,      // View your Google+ posts, comments, and stream
                                             PlusDomainsService.Scope.PlusStreamWrite,     // Manage your Google+ posts, comments, and stream
                                             PlusDomainsService.Scope.UserinfoEmail,       // View your email address
                                             PlusDomainsService.Scope.UserinfoProfile};    // View your basic profile info

            try
            {

                string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/PlusDomains");

                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore(credPath, true)).Result;

                var service = new PlusDomainsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "PlusDomains Authentication Sample",
                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                throw ex;
            }

        }

Media upload

  class MediaSample
    {
        /// <summary>
        /// Add a new media item to an album. The current upload size limitations are 36MB for a photo and 1GB for a video. Uploads do not count against quota if photos are less than 2048 pixels on their longest side or videos are less than 15 minutes in length.
        /// Documentation: https://developers.google.com/+/domains//v1/media/insert
        /// </summary>
        /// <param name="service">Valid authentcated PlusDomainsService</param>
        /// <param name="body">Valid Media Body</param>
        /// <param name="userId">The ID of the user to create the activity on behalf of.</param>
        /// <param name="collection"> Upload the media to share on Google+.</param>
        /// <returns>Media </returns>
        public static Media Insert(PlusDomainsService service, Media body, string userId, MediaResource.InsertRequest.CollectionEnum collection)
        {
            //Note Genrate Argument Exception (https://msdn.microsoft.com/en-us/library/system.argumentexception(loband).aspx)
            try
            {  
            return          service.Media.Insert(body, userId, collection).Execute();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Request Failed " + ex.Message);
                throw ex;
            }
         }

    }