私はoauth 2.0認証を行い、トークンを取得しました。ワークシートを追加しようとすると、service.insertでエラーが発生します。java-Errorを含むGoogle Apiスプレッドシート-com.google.gdata.util.ServiceException:メソッドが許可されていません
私はトークンを取得し、第二に、私は資格証明書を渡した後、シートを追加していところ2クラス1がある - :
クラス1
: -は、次のコードを参照してください。
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<String> 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 -->
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 <--
// Step 2: Exchange -->
GoogleTokenResponse response =
new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
code, REDIRECT_URI).execute();
// End of Step 2 <--
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());
}
// …
}
クラス2
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<SpreadsheetEntry> 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);
} // ...
}