您使用的不是 OAuth 2.0,而是使用 HMAC-SHA1 作为签名方法的 OAuth 1.0。要使用 OAuth 2.0,您至少需要 1.47.0 版的gdata-java-client库和 1.8.0-beta 版的google-oauth-java-client库。
使用google-api-java-client库提供了帮助类来处理 Google 的 OAuth 2.0 实现。
要检索 OAuth 2.0 凭据,您可以使用以下代码段:
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.jackson.JacksonFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class MyClass {
// Retrieve the CLIENT_ID and CLIENT_SECRET from an APIs Console project:
// https://code.google.com/apis/console
static String CLIENT_ID = "<YOUR_CLIENT_ID>";
static String CLIENT_SECRET = "<YOUR_CLIENT_SECRET>";
// Change the REDIRECT_URI value to your registered redirect URI for web
// applications.
static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
// Add other requested scopes.
static List<String> SCOPES = Arrays.asList("https://docs.google.com/feeds");
/**
* 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 <--
// 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());
}
// …
}
获得 OAuth 2.0 凭据后,您可以授权服务对象,如下所示:
// ...
import com.google.api.client.auth.oauth2.Credential;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.ServiceException;
// ...
import java.io.IOException;
import java.net.URL;
// ...
public class MyClass {
// …
/**
* Print document entries using the provided authorized DocsService.
*
* @param credential OAuth 2.0 credential to use to authorize the requests.
* @throws IOException
* @throws ServiceException
*/
static void printDocuments(Credential credential) throws IOException, ServiceException {
// Instantiate and authorize a new DocsService object.
DocsService service = new DocsService("<YOUR_APPLICATION_NAME>");
service.setOAuth2Credentials(credential);
// Send a request to the Documents List API to retrieve document entries.
URL feedUri = new URL("https://docs.google.com/feeds/default/private/full/");
DocumentListFeed feed = service.getFeed(feedUri, DocumentListFeed.class);
for (DocumentListEntry entry : feed.getEntries()) {
System.out.println("Title: " + entry.getTitle().getPlainText());
}
}
// ...
}
,可以从API 控制台CLIENT_ID
检索,并且必须与您的 API 项目中注册的匹配。CLIENT_SECRET
REDIRECT_URI