原文地址:http://bbs.seacat.cn/thread-890-1-1.html
授权请求
请求GoogleMirror API授权必须使用OAuth2.0认证.当用户离线时,您应该使用服务器端来代表应用程序需要访问的GoogleAPI。这种方法需要通过从客户端到服务器一次性授权代码,用于获取您服务器的访问和刷新令牌。
创建一个客户端ID和客户端密码
首先,你要为你的应用程序激活Google MirrorAPI.你在可以在谷歌API控制台(Google APIs Console)为你的API项目来激活。
1. 在谷歌API控制台(Google APIsConsole)创建一个项目
2. 在您的API项目选择服务(Services)选项卡,启用Google MirrorAPI
3. 在API项目选项卡选择API访问(API Access),并单击创建一个OAuth 2.0客户端ID(Createan OAuth 2.0 client ID)
4. 在品牌信息部分(BrandingInformation),为你的应用程序取一个名字,比如(我的眼镜服务),点击下一步(Next),提供一个可选的产品logo
5. 在客户端ID设置部分(Client IDSettings),请执行以下操作
a. 在应用程序类型(Applicationtype)选择Web应用程序(Web application)
b. 点击链接标题旁边的更多选项(more options),你的网站或主机名(Your site orhostname)
c. 列出你主机名的授权重定向URI(AuthorizedRedirect URIs)和JavaScript的源(JavaScriptorigins)
d. 点击创建客户端ID(Click CreateClient ID)
6. API访问(API Access)页面,找到Web应用程序客户端ID(Client ID forWeb applications),注意客户端ID(Client ID)和客户端密码(ClientSecret)的值
处理授权请求
当用户首次加载应用程序时,会弹出一个对话框,允许您的应用程序来访问他们的谷歌眼镜与请求的权限范围。初始授权后,如果应用程序的客户端ID更改或请求的范围发生了变化用户才会看到允许对话框。
认证用户
这个初始登录后会返回授权结果对象, 如果成功该对象会包含一个授权代码。
访问令牌交换授权代码
授权代码是一次性代码,您的服务器可以换取一个访问令牌。这个访问令牌传递到Google MirrorAPI允许应用程序在有限的时间内访问用户数据。
如果您的应用程序需要离线访问,第一次应用程序交换授权代码,它也收到一个刷新令牌,使用接收到的新的访问令牌后以前的令牌就过期了。您的应用程序要存储这种更新后的令牌。
重要:总是存储用户刷新令牌。如果你的应用需要一个新的刷新令牌必须放松一个参数来批准请求,这时用户就能在你的应用程序中看到一个允许的对话框了
下面的代码示例演示交换授权代码,离线访问一个访问令牌和刷新令牌存储。
为client_secrets.json文件替换CLIENTSECRETS_LOCATION 的值
JAVA:
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
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 com.google.api.services.oauth2.Oauth2;
import com.google.api.services.oauth2.model.Userinfo;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
// ...
class MyClass {
// Path to client_secrets.json which should contain a JSON document such as:
// {
// "web": {
// "client_id": "[[YOUR_CLIENT_ID]]",
// "client_secret": "[[YOUR_CLIENT_SECRET]]",
// "auth_uri": "https://accounts.google.com/o/oauth2/auth",
// "token_uri": "https://accounts.google.com/o/oauth2/token"
// }
// }
privatestaticfinal String CLIENTSECRETS_LOCATION = "client_secrets.json";
privatestaticfinal String REDIRECT_URI = "<YOUR_REGISTERED_REDIRECT_URI>";
privatestaticfinal List<String> SCOPES = Arrays.asList(
"https://www.googleapis.com/auth/glass.timeline",
"https://www.googleapis.com/auth/userinfo.profile");
privatestatic GoogleAuthorizationCodeFlow flow = null;
/**
* Exception thrown when an error occurred while retrieving credentials.
*/
publicstaticclass GetCredentialsException extends Exception {
protected String authorizationUrl;
/**
* Construct a GetCredentialsException.
*
* @param authorizationUrl The authorization URL to redirect the user to.
*/
public GetCredentialsException(String authorizationUrl) {
this.authorizationUrl = authorizationUrl;
}
/**
* Set the authorization URL.
*/
publicvoid setAuthorizationUrl(String authorizationUrl) {
this.authorizationUrl = authorizationUrl;
}
/**
* @return the authorizationUrl
*/
public String getAuthorizationUrl() {
return authorizationUrl;
}
}
/**
* Exception thrown when a code exchange has failed.
*/
publicstaticclass CodeExchangeException extends GetCredentialsException {
/**
* Construct a CodeExchangeException.
*
* @param authorizationUrl The authorization URL to redirect the user to.
*/
public CodeExchangeException(String authorizationUrl) {
super(authorizationUrl);
}
}
/**
* Exception thrown when no refresh token has been found.
*/
publicstaticclass NoRefreshTokenException extends GetCredentialsException {
/**
* Construct a NoRefreshTokenException.
*
* @param authorizationUrl The authorization URL to redirect the user to.
*/
public NoRefreshTokenException(String authorizationUrl) {
super(authorizationUrl);
}
}
/**
* Exception thrown when no user ID could be retrieved.
*/
privatestaticclass NoUserIdException extends Exception {
}
/**
* Retrieved stored credentials for the provided user ID.
*
* @param userId User's ID.
* @return Stored Credential if found, {@code null} otherwise.
*/
static Credential getStoredCredentials(String userId) {
// TODO: Implement this method to work with your database. Instantiate a new
// Credential instance with stored accessToken and refreshToken.
thrownew UnsupportedOperationException();
}
/**
* Store OAuth 2.0 credentials in the application's database.
*
* @param userId User's ID.
* @param credentials The OAuth 2.0 credentials to store.
*/
staticvoid storeCredentials(String userId, Credential credentials) {
// TODO: Implement this method to work with your database.
// Store the credentials.getAccessToken() and credentials.getRefreshToken()
// string values in your database.
thrownew UnsupportedOperationException();
}
/**
* Build an authorization flow and store it as a static class attribute.
*
* @return GoogleAuthorizationCodeFlow instance.
* @throws IOException Unable to load client_secrets.json.
*/
static GoogleAuthorizationCodeFlow getFlow() throws IOException {
if (flow == null) {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(jsonFactory,
MyClass.class.getResourceAsStream(CLIENTSECRETS_LOCATION));
flow =
new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, SCOPES)
.setAccessType("offline").setApprovalPrompt("force").build();
}
return flow;
}
/**
* Exchange an authorization code for OAuth 2.0 credentials.
*
* @param authorizationCode Authorization code to exchange for OAuth 2.0
* credentials.
* @return OAuth 2.0 credentials.
* @throws CodeExchangeException An error occurred.
*/
static Credential exchangeCode(String authorizationCode)
throws CodeExchangeException {
try {
GoogleAuthorizationCodeFlow flow = getFlow();
GoogleTokenResponse response =
flow.newTokenRequest(authorizationCode).setRedirectUri(REDIRECT_URI).execute();
return flow.createAndStoreCredential(response, null);
} catch (IOException e) {
System.err.println("An error occurred: " + e);
thrownew CodeExchangeException(null);
}
}
/**
* Send a request to the UserInfo API to retrieve the user's information.
*
* @param credentials OAuth 2.0 credentials to authorize the request.
* @return User's information.
* @throws NoUserIdException An error occurred.
*/
static Userinfo getUserInfo(Credential credentials)
throws NoUserIdException {
Oauth2 userInfoService =
new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credentials).build();
Userinfo userInfo = null;
try {
userInfo = userInfoService.userinfo().get().execute();
} catch (IOException e) {
System.err.println("An error occurred: " + e);
}
if (userInfo != null && userInfo.getId() != null) {
return userInfo;
} else {
thrownew NoUserIdException();
}
}
/**
* Retrieve the authorization URL.
*
* @param userId User's Google ID.
* @param state State for the authorization URL.
* @return Authorization URL to redirect the user to.
* @throws IOException Unable to load client_secrets.json.
*/
publicstatic String getAuthorizationUrl(String userId, String state) throws IOException {
GoogleAuthorizationCodeRequestUrl urlBuilder =
getFlow().newAuthorizationUrl().setRedirectUri(REDIRECT_URI).setState(state);
urlBuilder.set("user_id", userId);
return urlBuilder.build();
}
/**
* Retrieve credentials using the provided authorization code.
*
* This function exchanges the authorization code for an access token and
* queries the UserInfo API to retrieve the user's Google ID. If a
* refresh token has been retrieved along with an access token, it is stored
* in the application database using the user's Google ID as key. If no
* refresh token has been retrieved, the function checks in the application
* database for one and returns it if found or throws a NoRefreshTokenException
* with the authorization URL to redirect the user to.
*
* @param authorizationCode Authorization code to use to retrieve an access
* token.
* @param state State to set to the authorization URL in case of error.
* @return OAuth 2.0 credentials instance containing an access and refresh
* token.
* @throws NoRefreshTokenException No refresh token could be retrieved from
* the available sources.
* @throws IOException Unable to load client_secrets.json.
*/
publicstatic Credential getCredentials(String authorizationCode, String state)
throws CodeExchangeException, NoRefreshTokenException, IOException {
String userId = "";
try {
Credential credentials = exchangeCode(authorizationCode);
Userinfo userInfo = getUserInfo(credentials);
userId = userInfo.getId();
if (credentials.getRefreshToken() != null) {
storeCredentials(userId, credentials);
return credentials;
} else {
credentials = getStoredCredentials(userId);
if (credentials != null && credentials.getRefreshToken() != null) {
return credentials;
}
}
} catch (CodeExchangeException e) {
e.printStackTrace();
// Glass services should try to retrieve the user and credentials for the current
// session.
// If none is available, redirect the user to the authorization URL.
e.setAuthorizationUrl(getAuthorizationUrl(userId, state));
throw e;
} catch (NoUserIdException e) {
e.printStackTrace();
}
// No refresh token has been retrieved.
String authorizationUrl = getAuthorizationUrl(userId, state);
thrownew NoRefreshTokenException(authorizationUrl);
}