我正在使用 Google API Java 客户端http://code.google.com/p/google-api-java-client/并且能够成功获取 Android 的访问令牌。
// Google Accounts
credential = GoogleAccountCredential.usingOAuth2(this, CalendarScopes.CALENDAR);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
Run Code Online (Sandbox Code Playgroud)
由于我希望我的 Web 服务器进行离线 API 调用,因此我需要一个刷新令牌。我一直在广泛搜索,但还没有想出如何做到这一点。
理想情况下,我更喜欢在 WebView 上使用 Google API Java 客户端来获取刷新令牌(无需输入用户名或密码)。
任何帮助,将不胜感激!
google-api-java-client google-oauth google-oauth-java-client
我在使用Google OAuth Java Client调用twitter REST API时遇到问题。我能够正确地执行第一步:
然后,OAuth Javadoc会说:
通过设置OAuthParameters.token并将OAuthParameters用作HttpRequestInitializer,使用存储的访问令牌授权对受保护资源的HTTP请求。
在此步骤中,我遇到了问题。首先,如果我仅设置OAuthParameters.token值,我将得到一个null异常,因为未设置签名者,所以我现在拥有的是:
OAuthHmacSigner signer = new OAuthHmacSigner();
signer.clientSharedSecret=TWITTER_CONSUMER_SECRET;
String oauthToken = req.getParameter("oauth_token");
String oauthVerifier = req.getParameter("oauth_verifier");
OAuthGetAccessToken accessTokenRequest = new OAuthGetAccessToken(TWITTER_ACESS_TOKEN_URL);
accessTokenRequest.consumerKey=TWITTER_CONSUMER_KEY;
accessTokenRequest.signer=signer;
accessTokenRequest.transport=HTTP_TRANSPORT;
accessTokenRequest.temporaryToken=oauthToken;
accessTokenRequest.verifier=oauthVerifier;
OAuthCredentialsResponse credentials = accessTokenRequest.execute();
String token = credentials.token;
OAuthParameters params = new OAuthParameters();
params.token=token;
params.version="1.0";
params.consumerKey=TWITTER_CONSUMER_KEY;
params.signer=signer;
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(params);
HttpResponse twResponse = requestFactory.buildGetRequest(new GenericUrl("https://api.twitter.com/1.1/account/verify_credentials.json")).execute();
Run Code Online (Sandbox Code Playgroud)
结果始终是:
警告:验证错误:无法应对以下任何挑战:{} com.google.api.client.http.HttpResponseException:401 OK {“错误”:[{“消息”:“无法对您进行身份验证”,“代码“:32}]}
如果我尝试使用REST Chrome扩展程序工具通过Twitter OAuth工具提供的Authorization标头,则它可以正常工作,因此这不是帐户问题。当我将其更改为由Google OAuth Java客户端库计算出的Authorization标头值时,它不起作用。
我不明白我在做什么。
解决方案:按照@Arkanon提供的链接中的教程进行操作,我错过了通过以下方式刷新签名者令牌秘密的操作: …
我有一个Android应用程序,最终将用户生成的内容存储在Google云端存储分区中.但我无法通过我的应用代码执行此操作.代码如下所示:
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
KeyStore keystore = SecurityUtils.getPkcs12KeyStore();
keystore.load(resources.openRawResource(R.raw.secret), "***password***".toCharArray());
PrivateKey key = (PrivateKey)keystore.getKey("privatekey", "***password***".toCharArray());
credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(new JacksonFactory())
.setServiceAccountPrivateKey(key)
.setServiceAccountId("**************@developer.gserviceaccount.com")
.setServiceAccountScopes(Collections.singleton(StorageScopes.DEVSTORAGE_READ_WRITE))
.build();
credential.refreshToken();
String URI = "https://storage.googleapis.com/"+BUCKET_NAME;
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
Log.d("testing", "response content is: " + content);
new Storage.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("Doubts").build();
Run Code Online (Sandbox Code Playgroud)
我收到各种错误.其中之一是:
java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
Run Code Online (Sandbox Code Playgroud)
官方文档只是忽略了Android应用程序的用例.
android google-cloud-storage google-oauth google-oauth-java-client
我正在使用Java中的google analytics api.我有一个代码从刷新令牌中获取访问令牌.我们将刷新令牌存储在数据库中,并在请求时检索访问令牌.
密码更改后,我们的请求令牌变为无效.因此,我通过浏览网址(https://developers.google.com/oauthplayground)并按照说明生成了新的refreshtoken .我得到了新的刷新令牌.现在,当我执行代码来检索访问令牌时,它会抛出空指针异常.以下是代码:
RefreshTokenRequest request = new GoogleRefreshTokenRequest(HTTP_TRANSPORT, JSON_FACTORY, refreshToken,
this.clientId, this.clientSecret);
TokenResponse response = request.execute();
Run Code Online (Sandbox Code Playgroud)
调用execute方法时,会抛出以下异常:
java.lang.NullPointerException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.googleapis.auth.oauth2.GoogleRefreshTokenRequest.execute(GoogleRefreshTokenRequest.java:125)
at com.google.api.client.googleapis.auth.oauth2.GoogleRefreshTokenRequest.execute(GoogleRefreshTokenRequest.java:75)
at GARTest.main(GARTest.java:22)
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出我做错了什么?我可以从google oAuth UI获取访问令牌.
java google-analytics-api google-oauth-java-client google-oauth2
要求:我正在尝试通过Youtube Data API for Java将视频上传到我的Youtube频道.该请求是从托管在tomcat容器上的war文件发送的.我的应用程序不适用于外部用户,我只使用它来上传我自己生成的视频.在api文档和示例youtube代码片段的帮助下,我已成功设法在youtube上发布视频.
问题:问题是每当我尝试运行代码时,都会收到提示
请在您的浏览器中打开以下地址:https: //accounts.google.com/o/oauth2/auth? client_id =&redirect_uri = http:// localhost:8080/Callback&response_type = code&scope = https://www.googleapis.com /auth/youtube.upload
由于我在远程服务器上运行此代码,因此我无法始终在浏览器上打开此URL.由于我在Google控制台中注册了我的网络应用程序,并获得了一对客户端ID和密码以及JSON文件,因此Youtube必须允许我默认发布视频至少我的频道,不是吗?
我使用了Auth.java文件(在youtube java代码示例中提供),以下代码就是这件事发生的地方.
// Authorize.
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user@.com");
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
Run Code Online (Sandbox Code Playgroud)
请帮忙,因为这真的耗费了我很多的开发时间.
youtube youtube-api google-oauth youtube-data-api google-oauth-java-client
需要获得授权才能从Google Play开发者API获取信息。
我知道如何使用Postman进行此操作,但是实现授权要麻烦得多(重定向URL,处理重定向等),这些步骤就是您已经在Google Developer API Console中设置auth数据的步骤。
1.) GET https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=http://www.myurl.com/oauth2callback&client_id=1234567890.apps.googleusercontent.com
2.) get code which was sent to redirect url.
3.) POST https://accounts.google.com/o/oauth2/token
with
grant_type:authorization_code
code:[the code I got before]
client_id:1234567890.apps.googleusercontent.com
client_secret:[my client secret]
4.) Invoke GET https://www.googleapis.com/androidpublisher/v2/applications/packageName/purchases/subscriptions/subscriptionId/tokens/token
with:
Scope: https://www.googleapis.com/auth/androidpublisher
and:
access_token as query parameter I got before.
Run Code Online (Sandbox Code Playgroud)
现在,我要以编程方式完成所有这些操作。显然不是那么容易。我以为Google API客户端库会有所帮助,但我看不到这些库如何为我的用例提供帮助。
例如,像GoogleAuthorizationCodeFlow之类的类在请求时希望有一个用户ID,但现在不一定要有一个用户ID,因此我想知道如何以一种干净的方式使用此API。
有没有一种干净的方法可以通过一些API来更轻松/以编程方式处理OAuth2.0,以访问Google Play开发者API?否则,我必须手动实现它。
google-api google-oauth google-oauth-java-client google-oauth2
我正在尝试获取Google服务帐户的访问令牌.以下是我的代码 -
String SERVICE_ACCOUNT_EMAIL = "edited@developer.gserviceaccount.com";
List scope = new ArrayList();
scope.add("https://www.googleapis.com/auth/admin.directory.user");
String keyFile = "C:\\edited-privatekey.p12";
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(scope)
.setServiceAccountPrivateKeyFromP12File(new java.io.File(keyFile))
.build();
credential.refreshToken();
String accessTokens = credential.getAccessToken();
Run Code Online (Sandbox Code Playgroud)
虽然代码工作正常但我确实获得了访问令牌,但当我尝试使用Google Directory API"GET"Google Apps用户时,我会收到403 - 禁止响应代码.有人可以帮忙吗?
我知道GET用户的代码是正确的,因为它适用于Google Apps管理员生成的访问令牌.
google-api google-apps google-api-java-client google-oauth google-oauth-java-client
我要提前道歉,因为我可能会丢失一些信息,以便让任何人知道我的设置是正确的。请让我知道我还需要指定什么。我在一所大学工作,我们通过 Google 托管学生电子邮件帐户。他们经常忘记密码并不得不重置密码。如果他们验证了足够的有关自己的信息,我们有一个页面可以设置他们的密码。Google 已弃用我们一直使用的代码,转而使用他们的 Directory API。我的任务是转换这个旧代码:
//changes a password
@Override
public void reset ( final String username, final String password ) throws ResetException
{
try
{
Validate.notEmpty( username );
Validate.notEmpty( password );
final AppsForYourDomainClient client = ClientFactory.getClient(); //admin-user account
final UserEntry entry = client.retrieveUser( username );
Validate.isTrue( !entry.getLogin().getSuspended(), "Account is suspended." );
entry.getLogin().setPassword( password );
client.updateUser( username, entry );
}
catch ( final Exception e )
{
throw new ResetException( e );
}
}
Run Code Online (Sandbox Code Playgroud)
使用他们的新 API 进行一些操作。我读过很多文档和几个示例,但似乎没有一个有帮助。我已通过他们的管理控制台为我们的管理员帐户启用了 Admin SDK,并注册了我的应用程序并从他们的开发人员控制台获取了密钥,但我似乎无法收到任何返回我想要的内容的请求。现在我只是想获取用户列表:
public …Run Code Online (Sandbox Code Playgroud) google-oauth ×5
java ×3
google-api ×2
android ×1
google-apps ×1
oauth ×1
twitter ×1
youtube ×1
youtube-api ×1