Welcome to ASP.NET Guild

Be sure to come back often and tell others. If you have any tips, tricks, examples, please email them to me at chris.williams@techguilds.com and I will post them. Check out our ASP.NET QuickStart and C# QuckStart Libraries. Below is my latest articles.

Friday, December 15, 2017

Reading and Writing files to Google Drive

It has been a while since I posted but I have been playing with Google Drive and accessing it from a console application and had to search in a few places to find the right way to do it. The documentation tells you to download the .JSON file and that is helpful.  You can find the sample on how to get a list of files from Google Drive in a Console application.

This is a good start but I was getting errors related to the Certificate:

"At least one client secret (Installed or Web) should be set"

The problem is that the code provided has you using UserCredentials and some funky streaming stuff.  The credentials can be read from the stream like this into a Google Credentials object.

            GoogleCredential credential = null;

            using (var stream = new FileStream(credentialsFileName, FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream);
                credential = credential.CreateScoped(scopes);
            }

You then use that Google Credentials along with your application name to get the service:

            // Create Drive API service.
            return new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = applicationName,
            });

Now after that fix, the original example works. I am assuming that since the article was written this has changed and the article has not been updated.  After applying this I do get the list of files from the article.

Scopes

A scope is what determines what the calls made using the service can do. In general there are 2 scopes: Read-Only and Full Access. There are other scopes you can use as well.

If you want to do more than read from the drive then you will need to change your scope.  

        string[] ReadOnlyScope = { DriveService.Scope.DriveReadonly }; // Read Only
        public static string[] FullAccessScope = { DriveService.Scope.Drive }; // Full Access


Permissions

It took a while to find how permissions work but I did find an article on Managing Shares that does go into it. It boils down to 2 types of permission: user and domain. A user permission assigns an individual user access. A domain permission assigns a domain access eg. "sitecore.net".  A permission has a role. The article describes the roles as "reader" and "writer".  This is a text string so possibly there are other roles as well.

Now personally, I have got the user permissions to work but when I try domain permissions I get an error. If you have successfully got domain working can you provide an example or take a look at the functions on GitHub for Domain Permission to see if there is something I am doing wrong?  I am passing "readwatchcreate.com" as domain. 

GitHub Libraries

I have created a GoogleAPIs public repository on GitHub. There are 2 folders. One for those still using .NET Framework which will not be updated and a DotCore folder with the new version built on .NET Standard and .NET Core. This includes a base library that has the code to connect to the API, a library that wraps some Google Drive functions to help you, and a unit test console app that will show sample calls and do positive and negative testing of the library. As I continue my learning of Google APIs, I will be adding more libraries for GoogleSheet and other API sections.


Monday, May 01, 2017

Introducing the ASP.NET Facebook Login QuickStart

Logging into Facebook is actually quite simple especially if you use our new ASP.NET Facebook Login Quickstart.

If you do not have a copy already email chris.williams@readwatchcreate.com to get instructions on how to access.

Installation

The first step is setting up your database.


  1. Open the database scripts in the QuickStart zip that start with CREATE_TABLE_.  Ensure they table names match your application prefixes for consistency.
  2. Open the database scripts in the QuickStart zip that start with CREATE_PROC_.  Ensure the table names match the tables in the CREATE_TABLE_ scripts in step 1.  
  3. Rename the procs to match your application prefixes for consistency. 
  4. Add the appSetting entries from the web.config in the Quickstart in the Web.Config file for your project. Make sure you change the stored procedure names in the appSettings to match your stored procedure names.
The second step is configure your ASP.NET application to use the plug-in.

  1. Add standard button or link button to page and implement click event.
  2. Call the LoginToFacebook function on this class with a call similar to the following but replace {my_site_name} with your site url.

    You may also want to change the access you desire:

    ASPNET.Authentication.Facebook.FacebookLogin.LoginToFacebook(ConfigurationManager.AppSettings["FacebookAppId"], "http://{my_site_name}/fb.aspx", "offline_access, email, user_likes, friends_likes");
  3. Create a page called fb.aspx and on load add the following code:

    if (Request["code"] != null) Session["AccessToken"] = FacebookLogin.GetAccessToken(ConfigurationManager.AppSettings["FacebookAppId"], ConfigurationManager.AppSettings["FacebookAppSecret"], Request["code"], ConfigurationManager.AppSettings["FacebookReturnUrl"]);
  4. Next you need to add the configuration keys to the web.config in teh appSettings section. These are provided by Facebook when you register your application:

    <add key="FacebookReturnUrl" value="http://{my site}/fb.aspx"/>
    <add key="FacebookAppId" value="{Likely a 15 digit number}"/>
    <add key="FacebookAppSecret" value="{This is a guid with no dashes}"/>
  5. Use the code returned to get the profile info back in JSON then use the helper functions to get each attribute you wish:

    string userProfileJSON = CXFacebook.GetUserProfile(Session["AccessToken"].ToString());
    ///     string id = CXFacebook.GetUserProfileId(userProfileJSON);
    ///     string name = CXFacebook.GetUserProfileName(userProfileJSON);
    ///     string firstName = CXFacebook.GetUserProfileFirstName(userProfileJSON);
    ///     string lastName = CXFacebook.GetUserProfileLastName(userProfileJSON);
    ///     string email = CXFacebook.GetUserProfileEmail(userProfileJSON);
    ///     string birthday = CXFacebook.GetUserProfileBirthday(userProfileJSON);
    ///     string link = CXFacebook.GetUserProfileLink(userProfileJSON);
    ///     string locale = CXFacebook.GetUserProfileLocale(userProfileJSON);
    ///     string quote = CXFacebook.GetUserProfileQuotes(userProfileJSON);
    ///     string userName = CXFacebook.GetUserProfileUserName(userProfileJSON);
    ///     string timeZone = CXFacebook.GetUserProfileTimezone(userProfileJSON);




Are you a .NET Developer or Contractor interested in working with Sitecore or Dynamics CRM?

Apply for our Mentorship Program. If accepted, we will mentor you on Sitecore and provide you with project to help you build your skills and make some money at the same time. If you are interested send your resume with details on why you want to work with Sitecore or Dynamics CRM to: Chris Williams - chris.williams@techguilds.com or Dennis Augustine - dennis.augustine@techguilds.com We look forward to working with you to achieve your goals.