Question Asker
Question Asker

Reputation: 199

Google Sheets API Get List of Sheets ERROR: Parsing Failed

I am creating an application that will eventually insert data into a Google Sheets sheet. However, I cannot even get a list of the sheets at the moment. Below is my code. What am I doing wrong?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

#region Google Spreadsheets specific imports
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;
using Google.GData.Tools;
using System.Diagnostics;
#endregion

namespace Google_SpreadsheetApplication
{
    public partial class Form1 : Form
    {



        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }
        OAuth2Parameters parameters = new OAuth2Parameters();

        private void button1_Click(object sender, EventArgs e)
        {
            ////////////////////////////////////////////////////////////////////////////
            // STEP 1: Configure how to perform OAuth 2.0
            ////////////////////////////////////////////////////////////////////////////

            // TODO: Update the following information with that obtained from
            // https://code.google.com/apis/console. After registering
            // your application, these will be provided for you.

            string CLIENT_ID = "1097084440934-ll1bdhbbvmtgmj23gmlkj0hcgm18o5rv.apps.googleusercontent.com";

            // This is the OAuth 2.0 Client Secret retrieved
            // above.  Be sure to store this value securely.  Leaking this
            // value would enable others to act on behalf of your application!
            string CLIENT_SECRET = "My Secret";

            // Space separated list of scopes for which to request access.
            string SCOPE = "https://spreadsheets.google.com/feeds/cells/key/spreadsheets/private/basic";

            // This is the Redirect URI for installed applications.
            // If you are building a web application, you have to set your
            // Redirect URI at https://code.google.com/apis/console.
            string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

            ////////////////////////////////////////////////////////////////////////////
            // STEP 2: Set up the OAuth 2.0 object
            ////////////////////////////////////////////////////////////////////////////

            // OAuth2Parameters holds all the parameters related to OAuth 2.0.

            // Set your OAuth 2.0 Client Id (which you can register at
            // https://code.google.com/apis/console).
            parameters.ClientId = CLIENT_ID;

            // Set your OAuth 2.0 Client Secret, which can be obtained at
            // https://code.google.com/apis/console.
            parameters.ClientSecret = CLIENT_SECRET;

            // Set your Redirect URI, which can be registered at
            // https://code.google.com/apis/console.
            parameters.RedirectUri = REDIRECT_URI;

            ////////////////////////////////////////////////////////////////////////////
            // STEP 3: Get the Authorization URL
            ////////////////////////////////////////////////////////////////////////////

            // Set the scope for this particular service.
            parameters.Scope = SCOPE;

            // Get the authorization url.  The user of your application must visit
            // this url in order to authorize with Google.  If you are building a
            // browser-based application, you can redirect the user to the authorization
            // url.
            string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
            Process.Start(authorizationUrl);


        }

        private void button2_Click(object sender, EventArgs e)
        {
            parameters.AccessCode = textBox1.Text.ToString();

            ////////////////////////////////////////////////////////////////////////////
            // STEP 4: Get the Access Token
            ////////////////////////////////////////////////////////////////////////////

            // Once the user authorizes with Google, the request token can be exchanged
            // for a long-lived access token.  If you are building a browser-based
            // application, you should parse the incoming request token from the url and
            // set it in OAuthParameters before calling GetAccessToken().
            OAuthUtil.GetAccessToken(parameters);
            string accessToken = parameters.AccessToken;
            parameters.AccessToken = accessToken;
            MessageBox.Show("OAuth Access Token: " + accessToken);
            ////////////////////////////////////////////////////////////////////////////
            // STEP 5: Make an OAuth authorized request to Google
            ////////////////////////////////////////////////////////////////////////////

            // Initialize the variables needed to make the request
            GOAuth2RequestFactory requestFactory =
                new GOAuth2RequestFactory(null, "GPLHS Activity Monitor", parameters);
            SpreadsheetsService service = new SpreadsheetsService("GPLHS Activity Monitor");
            service.RequestFactory = requestFactory;
            //SpreadsheetsService service = new SpreadsheetsService("GPLHS Activity Monitor");

            // TODO: Authorize the service object for a specific user (see other sections)

            // Instantiate a SpreadsheetQuery object to retrieve spreadsheets.
            SpreadsheetQuery query = new SpreadsheetQuery();

            // Make a request to the API and get all spreadsheets.
            SpreadsheetFeed feed = service.Query(query);

            if (feed.Entries.Count == 0)
            {
                // TODO: There were no spreadsheets, act accordingly.
                MessageBox.Show("Nothin to Show!");
            }

            // TODO: Choose a spreadsheet more intelligently based on your
            // app's needs.
            SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
            MessageBox.Show(spreadsheet.Title.Text);

            // Get the first worksheet of the first spreadsheet.
            // TODO: Choose a worksheet more intelligently based on your
            // app's needs.
            WorksheetFeed wsFeed = spreadsheet.Worksheets;
            WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

            // Update the local representation of the worksheet.
            worksheet.Title.Text = "Updated Worksheet";
            worksheet.Cols = 5;
            worksheet.Rows = 15;

            // Send the local representation of the worksheet to the API for
            // modification.
            worksheet.Update();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SpreadsheetsService service = new SpreadsheetsService("GPLHS Activity Monitor");

            // TODO: Authorize the service object for a specific user (see other sections)

            // Instantiate a SpreadsheetQuery object to retrieve spreadsheets.
            SpreadsheetQuery query = new SpreadsheetQuery();

            // Make a request to the API and get all spreadsheets.
            SpreadsheetFeed feed = service.Query(query);

            if (feed.Entries.Count == 0)
            {
                // TODO: There were no spreadsheets, act accordingly.
                MessageBox.Show("Nothin to Show!");
            }

            // TODO: Choose a spreadsheet more intelligently based on your
            // app's needs.
            SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
            MessageBox.Show(spreadsheet.Title.Text);

            // Get the first worksheet of the first spreadsheet.
            // TODO: Choose a worksheet more intelligently based on your
            // app's needs.
            WorksheetFeed wsFeed = spreadsheet.Worksheets;
            WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

            // Update the local representation of the worksheet.
            worksheet.Title.Text = "Updated Worksheet";
            worksheet.Cols = 5;
            worksheet.Rows = 15;

            // Send the local representation of the worksheet to the API for
            // modification.
            worksheet.Update();

        }
    }
}

Note: I get as far as getting an authentication code/token. My Error: https://i.sstatic.net/B4H39.png

Update: I added step 5 and put the query right after authorization. I now get this error:Additional information: Execution of request failed: https://spreadsheets.google.com/feeds/spreadsheets/private/full It says that it was unauthorized.

Upvotes: 1

Views: 825

Answers (1)

Question Asker
Question Asker

Reputation: 199

I am going to answer my own question here. In the code where SCOPE is specified, the value should be: https://spreadsheets.google.com/feeds not https://spreadsheets.google.com/feeds/cells/key/spreadsheets/private/basic. Hope this helps someone!

Upvotes: 2

Related Questions