2012-11-06 42 views
5

Tôi muốn lấy "mã thông báo truy cập" mới dựa trên "mã thông báo làm mới" được lưu trong cơ sở dữ liệu.(JAVA) Phân tích API Google - không thể lấy "mã thông báo truy cập" mới bằng cách sử dụng "mã thông báo làm mới"

Đây là mã tôi đã viết:

GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder() 
     .setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY) 
     .setClientSecrets(CLIENT_ID, CLIENT_SECRET); 
credentialBuilder.addRefreshListener(new MyCredentialRefreshListener()); 

credential = credentialBuilder.build(); 
credential.setRefreshToken("saved_refresh_token_from_database"); 

try { 
    credential.refreshToken(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 


class MyCredentialRefreshListener implements CredentialRefreshListener { 
    public void onTokenResponse(Credential cr, TokenResponse tr) { 
     System.out.println("Credential was refreshed successfully."); 
    } 

    public void onTokenErrorResponse(Credential cr, TokenErrorResponse tr) { 
     System.out.println(tr); 
    } 
} 

tôi nhận được tin nhắn này:

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad

Yêu cầu {"error": "invalid_grant"}

Tôi sử dụng cùng CLIENT_ID, CLIE NT_SECRET và "mã thông báo làm mới" trong tập lệnh php và ở đó tôi đã quản lý để nhận "mã thông báo truy cập" mới bằng cách sử dụng "mã thông báo làm mới" khi "mã thông báo truy cập" hết hạn.

Tôi đã viết mã dựa trên javadoc.google-oauth-java-client.

Mọi người ở đây đều biết cách sửa đổi mã để lấy mã thông báo truy cập mới?

Xin cảm ơn trước.

CẬP NHẬT: vấn đề là tôi đã lưu trong cơ sở dữ liệu refresh_token mà không thực hiện json_decode trên nó và nó chứa "\" được coi là ký tự thoát trong JSON.

+0

Tôi đã thử mã của bạn và mã này đã hoạt động đối với tôi. Tôi nghĩ rằng đó là vấn đề về dữ liệu/định dạng với mã thông báo làm mới của bạn. – user1697575

Trả lời

2

Dường như bạn có thể đã tìm thấy một số tài liệu lỗi thời. Liên kết JavaDoc bạn dùng cho phiên bản 1.8 của thư viện khách. Phiên bản hiện tại là 1.12.

Tác giả thư viện khách hàng khuyên bạn nên sử dụng GoogleAuthorizationCodeFlow để quản lý thông tin đăng nhập OAuth của bạn. Nó sẽ tự động làm mới. Nếu bạn đi theo con đường này, mã sẽ trông giống như sau:

// Create the flow 
AuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow.Builder(
    new UrlFetchTransport(), new JacksonFactory(), CLIENT_ID, CLIENT_SECRET, 
    Collections.singleton(OAUTH_SCOPES)) 
    .setAccessType("offline") 
    .setCredentialStore(new AppEngineCredentialStore()) 
    .build(); 

// User Id: e.g. from session 
Credential credential = authorizationCodeFlow.loadCredential(USER_ID);  

// Make your API call. This example uses the Google+ API 
// If the access token has expired, it will automatically refresh 
Plus plus = new Plus(new UrlFetchTransport(), new JacksonFactory(), credential) 
    .activities().list("me", "public").execute(); 
1

nếu bạn nhận được trạng thái http 400 và thông báo "invalid_grant". Tôi nghĩ bạn nên kiểm tra bản sao của mình là HTTP_TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET.

Các vấn đề liên quan