Google Api spreadsheets with java- Error -com.google.gdata.util.ServiceException: Method Not Allowed
<p>I have done the oauth 2.0 authentication and I got the token. Now when I am trying to add a worksheet I am getting an error on service.insert.</p>

<p>There are 2 classes one where I get the token and in second where I am passing the credentials and then adding the sheet -:</p>

<p>Please see the following code -:</p>

<p>Class 1</p>

<pre><code>import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.gdata.util.ServiceException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class OAuth2Sample{

  // Retrieve the CLIENT_ID and CLIENT_SECRET from an APIs Console project:
  //     https://code.google.com/apis/console
  static String CLIENT_ID = "cilent id";
  static String CLIENT_SECRET = "client secret
";
  // Change the REDIRECT_URI value to your registered redirect URI for web
  // applications.
  static String REDIRECT_URI = "http://www.google.com";
  // Add other requested scopes.
  static List&lt;String&gt; SCOPES = Arrays.asList("https://spreadsheets.google.com/feeds");


public static void main (String args[]) throws IOException, ServiceException {
    Credential credencial = getCredentials();
    Abc.printDocuments(credencial);
}


  /**
   * Retrieve OAuth 2.0 credentials.
   * 
   * @return OAuth 2.0 Credential instance.
   */
  static Credential getCredentials() throws IOException {
    HttpTransport transport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    // Step 1: Authorize --&gt;
    String authorizationUrl =
        new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URI, SCOPES).build();

    // Point or redirect your user to the authorizationUrl.
    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);

    // Read the authorization code from the standard input stream.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is the authorization code?");
    String code = in.readLine();
    // End of Step 1 &lt;--

    // Step 2: Exchange --&gt;
    GoogleTokenResponse response =
        new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
            code, REDIRECT_URI).execute();
    // End of Step 2 &lt;--
       System.out.println(response);
    // Build a new GoogleCredential instance and return it.
    return new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET)
        .setJsonFactory(jsonFactory).setTransport(transport).build()
     .setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken());
  }

  // …
}
</code></pre>

<p>Class 2</p>

<pre><code>    import com.google.api.client.auth.oauth2.Credential;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.PlainTextConstruct;

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.*;
import java.util.*;
import com.google.gdata.util.ServiceException;

// ...



    // ...

    public class Abc {
      // …

      static void printDocuments(Credential credential) throws IOException, ServiceException {
        // Instantiate and authorize a new SpreadsheetService object.

          SpreadsheetService service = new SpreadsheetService("Application-Name"); // Dont know exactly what comes here
          service.setProtocolVersion(SpreadsheetService.Versions.V3); // It's important to specify the version


          System.out.println(credential);
          service.setOAuth2Credentials(credential);
                // TODO: Authorize the service object for a specific user (see other sections)

                // Define the URL to request.  This should never change.
                System.out.println("hello");
                URL SPREADSHEET_FEED_URL = new URL(
                    "https://spreadsheets.google.com/feeds/worksheets/1svU1AqLz0wPpUeYTrTx4QALbV3Mb4GM3YJklrl_BAfQ/public/full");
                System.out.println("hello1");
                // Make a request to the API and get all spreadsheets.
                SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
                    SpreadsheetFeed.class);
                System.out.println(feed);
                List&lt;SpreadsheetEntry&gt; spreadsheets = feed.getEntries();
                System.out.println(spreadsheets);
                if (spreadsheets.size() == 0) {
                  // TODO: There were no spreadsheets, act accordingly.
                }

                // TODO: Choose a spreadsheet more intelligently based on your
                // app's needs.
                SpreadsheetEntry spreadsheet = spreadsheets.get(0);
                System.out.println(spreadsheet.getTitle().getPlainText());

                // Create a local representation of the new worksheet.
                WorksheetEntry worksheet = new WorksheetEntry();
                worksheet.setTitle(new PlainTextConstruct("New Worksheet"));
                worksheet.setColCount(10);
                worksheet.setRowCount(20);

                // Send the local representation of the worksheet to the API for
                // creation.  The URL to use here is the worksheet feed URL of our
                // spreadsheet.
                URL worksheetFeedUrl = spreadsheet.getWorksheetFeedUrl();
                System.out.println(worksheetFeedUrl);
                service.insert(worksheetFeedUrl, worksheet);

      }  // ...
    }
</code></pre>