使用刷新令牌访问Google Drive API,无浏览器,401错误 [英] Access Google Drive API with refresh token, no browser, 401 error

查看:463
本文介绍了使用刷新令牌访问Google Drive API,无浏览器,401错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用刷新令牌(来自OAuth Playground)发布GoogleDrive API(我自己的a / c) - 如下所示。我正在使用我的刷新令牌和访问令牌进行脱机访问,但我遇到了401未经授权的情况。我的代码基于参考文献belwoand google-api javadocs对建立离线请求的建议



我不想使用浏览器,我希望运行这从服务器端应用程序上传到我自己的a / c。

  ref:http://stackoverflow.com / questions / 10533203 / fetching-access-token-from-refresh-token-using-java 



  public static void main(String [] args)throws IOException {
HttpTransport TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();

字符串refreshTokenString =xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; // from console

字符串accessTokenString

=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy; //从控制台

GoogleCredential凭证= createCredentialWithRefreshToken(
TRANSPORT,JSON_FACTORY,new TokenResponse()。setRefreshToken(refreshTokenString));
credential.setAccessToken(accessTokenString);


//执行离线访问的HTTP请求toTokenString

//执行HTTP GET请求以撤消当前令牌。
HttpResponse response = TRANSPORT.createRequestFactory()
.buildGetRequest(new GenericUrl(
String.format(
https://www.googleapis.com/drive/v2/changes ,
credential.getAccessToken())))。execute();

System.out.println(RESPONSE:+ response);







$ b public static GoogleCredential createCredentialWithRefreshToken(HttpTransport transport,
JsonFactory jsonFactory,TokenResponse tokenResponse){

HttpTransport TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
返回新的GoogleCredential.Builder()。setTransport(传输)
.setJsonFactory(JSON_FACTORY)
.setClientSecrets(CLIENT_ID,CLIENT_SECRET)
.build()
.setFromTokenResponse (tokenResponse);
}

与错误:

  {
error:{
errors:[
{
domain:global,
reason:required,
message:需要登录,
locationType:header,
location:Authorization
}
code:401,
message:需要登录
}
}

我使用有效的刷新令牌和访问令牌(从游戏机控制台运行),但是在从Java应用程序运行时,我需要登录401。我需要从服务器直接将日常备份到驱动器。

解决方案

不能使用Playgorund中的访问或刷新令牌在你的应用程序。这些代币由谷歌发布,只在操场上使用。您获得的令牌与客户端ID和客户端密钥相关联。显然,您的CLIENT_ID和CLIENT_SECRET与Google Playground应用程序使用的不同。



您真的需要您的应用程序请求这些令牌,因此您第一次需要一个浏览器,以便用户可以接受你的应用程序(而不是Google Playground)正在让你的信息存储在谷歌。这称为用户同意,并显示在此页面的小图中:使用OAuth 2.0进行网络管理服务器应用程序

然后,当您有应用程序的刷新令牌时。您的日常备份不再需要浏览器。
您可以使用您的代码和适用于您的应用的有效刷新令牌,如您的帖子中所示。


I am trying to access GoogleDrive API (my own a/c) using the refresh token --issued from OAuth Playground -- like below. I am using my refresh token and access token for offline access but I am getting a 401 unauthorized. My code is based on reference belwoand google-api javadocs recommendation for building an offline request

I do not wish to use a browser, i wish to run this from a server side app to upload to my own a/c.

ref: http://stackoverflow.com/questions/10533203/fetching-access-token-from-refresh-token-using-java

Code:

public static void main(String[] args) throws IOException {
        HttpTransport TRANSPORT = new NetHttpTransport();
        JsonFactory JSON_FACTORY = new JacksonFactory();

        String refreshTokenString="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //from console

       String accessTokenString 

="yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"; //from console

    GoogleCredential credential = createCredentialWithRefreshToken(
            TRANSPORT, JSON_FACTORY, new TokenResponse().setRefreshToken(refreshTokenString));
    credential.setAccessToken("accessTokenString");


    //exeucute HTTP request for offline accessTokenString

    // Execute HTTP GET request to revoke current token.
    HttpResponse response = TRANSPORT.createRequestFactory()
            .buildGetRequest(new GenericUrl(
                    String.format(
                            "https://www.googleapis.com/drive/v2/changes",
                            credential.getAccessToken()))).execute();

    System.out.println("RESPONSE: " +response);

}






 public static GoogleCredential createCredentialWithRefreshToken(HttpTransport transport,
                                                                    JsonFactory jsonFactory, TokenResponse tokenResponse) {

    HttpTransport TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    return new GoogleCredential.Builder().setTransport(transport)
            .setJsonFactory(JSON_FACTORY)
            .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
            .build()
            .setFromTokenResponse(tokenResponse);
}

And the error:

    {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

I am using a valid refresh token and access token (works from playground console) but i get login required 401 here when running from Java app. I requires this for daily backups from server directly to Drive.

解决方案

You can't use the access or refresh token from the Playgorund in your application. These tokens where issued by google only to be used in the playground. The tokens you get are tied to a Client Id and Client Secret. Obviously your CLIENT_ID and CLIENT_SECRET are different from the ones which are used by the Google Playground application.

You really need your application to request these tokens, so for the first time you need a browser, so that the user can accept that YOUR app (not Google Playground) is getting your information stored at google. This is called "user consent", and shown in the little diagram at this page: Using OAuth 2.0 for Web Server Applications)

Then, when you have the refresh token for your application. You don't need the browser any more for your daily backups. You can use your code with a valid refresh token for your app, as presented in your post.

这篇关于使用刷新令牌访问Google Drive API,无浏览器,401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆