gpt4 book ai didi

java - 用于设备 Java 库的 Google API OAuth 2.0

转载 作者:行者123 更新时间:2023-11-30 08:09:40 24 4
gpt4 key购买 nike

我需要为此处描述的设备实现 OAuth 2.0 流程:

https://developers.google.com/identity/protocols/OAuth2ForDevices

我在适用于 Java 的 Google API 客户端库中找不到对此的任何实现。

例如,支持已安装的应用程序流 (https://developers.google.com/identity/protocols/OAuth2InstalledApp),如本例所示:

https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#installed_applications

但对于没有浏览器的设备则没有...

我应该从头开始实现 API 还是缺少某些东西?

谢谢!

最佳答案

我遇到了同样的问题。我需要弄清楚 api 客户端库的结构。不管怎样,这里有一个用于设备的 oauth 2.0 的示例代码。

它打印出用户代码和验证网址。转到验证网址并输入用户代码。在您授权该应用程序后,它会获得访问和刷新 token 。

public static class OAuthForDevice{


private static final String TOKEN_STORE_USER_ID = "butterflytv";

public static String CLIENT_ID = "Your client id";
public static String CLIENT_SECRET = "your client secredt";

public static class Device {
@Key
public String device_code;
@Key
public String user_code;
@Key
public String verification_url;
@Key
public int expires_in;
@Key
public int interval;
}

public static class DeviceToken {
@Key
public String access_token;
@Key
public String token_type;
@Key
public String refresh_token;
@Key
public int expires_in;
}

public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

/**
* Define a global instance of the JSON factory.
*/
public static final JsonFactory JSON_FACTORY = new JacksonFactory();


public static void main(String[] args) {
getCredential();
}

public static Credential getCredential() {
Credential credential = null;
try {
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home")));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("youtube_token");

credential = loadCredential(TOKEN_STORE_USER_ID, datastore);
if (credential == null) {

GenericUrl genericUrl = new GenericUrl("https://accounts.google.com/o/oauth2/device/code");


Map<String, String> mapData = new HashMap<String, String>();
mapData.put("client_id", CLIENT_ID);
mapData.put("scope", "https://www.googleapis.com/auth/youtube.upload");
UrlEncodedContent content = new UrlEncodedContent(mapData);
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
request.setParser(new JsonObjectParser(JSON_FACTORY));
}
});
HttpRequest postRequest = requestFactory.buildPostRequest(genericUrl, content);

Device device = postRequest.execute().parseAs(Device.class);
System.out.println("user code :" + device.user_code);
System.out.println("device code :" + device.device_code);
System.out.println("expires in:" + device.expires_in);
System.out.println("interval :" + device.interval);
System.out.println("verification_url :" + device.verification_url);


mapData = new HashMap<String, String>();
mapData.put("client_id", CLIENT_ID);
mapData.put("client_secret", CLIENT_SECRET);
mapData.put("code", device.device_code);
mapData.put("grant_type", "http://oauth.net/grant_type/device/1.0");

content = new UrlEncodedContent(mapData);
postRequest = requestFactory.buildPostRequest(new GenericUrl("https://accounts.google.com/o/oauth2/token"), content);

DeviceToken deviceToken;
do {
deviceToken = postRequest.execute().parseAs(DeviceToken.class);

if (deviceToken.access_token != null) {
System.out.println("device access token: " + deviceToken.access_token);
System.out.println("device token_type: " + deviceToken.token_type);
System.out.println("device refresh_token: " + deviceToken.refresh_token);
System.out.println("device expires_in: " + deviceToken.expires_in);
break;
}
System.out.println("waiting for " + device.interval + " seconds");
Thread.sleep(device.interval * 1000);

} while (true);


StoredCredential dataCredential = new StoredCredential();
dataCredential.setAccessToken(deviceToken.access_token);
dataCredential.setRefreshToken(deviceToken.refresh_token);
dataCredential.setExpirationTimeMilliseconds((long)deviceToken.expires_in*1000);

datastore.set(TOKEN_STORE_USER_ID, dataCredential);

credential = loadCredential(TOKEN_STORE_USER_ID, datastore);
}
}
catch (Exception e) {
e.printStackTrace();
}

return credential;
}

public static Credential loadCredential(String userId, DataStore<StoredCredential> credentialDataStore) throws IOException {
Credential credential = newCredential(userId, credentialDataStore);
if (credentialDataStore != null) {
StoredCredential stored = credentialDataStore.get(userId);
if (stored == null) {
return null;
}
credential.setAccessToken(stored.getAccessToken());
credential.setRefreshToken(stored.getRefreshToken());
credential.setExpirationTimeMilliseconds(stored.getExpirationTimeMilliseconds());
}
return credential;
}

private static Credential newCredential(String userId, DataStore<StoredCredential> credentialDataStore) {

Credential.Builder builder = new Credential.Builder(BearerToken
.authorizationHeaderAccessMethod()).setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setTokenServerEncodedUrl("https://accounts.google.com/o/oauth2/token")
.setClientAuthentication(new ClientParametersAuthentication(CLIENT_ID, CLIENT_SECRET))
.setRequestInitializer(null)
.setClock(Clock.SYSTEM);

builder.addRefreshListener(
new DataStoreCredentialRefreshListener(userId, credentialDataStore));

return builder.build();
}

}

关于java - 用于设备 Java 库的 Google API OAuth 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32254399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com