GoogleCloudPlatform / android-docs-samples

Apache License 2.0
375 stars 596 forks source link

Error: OAuth2Credentials instance does not support refreshing the access token. How to create a AccessToken instance with token value string? #73

Open sydridgmLee opened 6 years ago

sydridgmLee commented 6 years ago

The sample runs successfully. But Alternatively, you should get the access token on the server side, and supply client app with it.

I set up a server and create RESTful api for app to get the access token. {"token":"1234567890sdertyuikjhgfghjk....."}

And also use api: https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={token} to get the token info: { "issued_to": "102073270616313859663", "audience": "102073270616313859663", "scope": "https://www.googleapis.com/auth/cloud-platform", "expires_in": 3600, "access_type": "offline" }

Then create a Token instance: AccessToken token = new AccessToken(tokenValue, expiresIn);

But following error:

07-04 20:14:07.395 3023-3067/com.google.cloud.android.speech E/SpeechService: Error calling the API.
    io.grpc.StatusRuntimeException: UNKNOWN
        at io.grpc.Status.asRuntimeException(Status.java:543)
        at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:395)
        at io.grpc.ClientInterceptors$CheckedForwardingClientCall.start(ClientInterceptors.java:202)
        at io.grpc.stub.ClientCalls.startCall(ClientCalls.java:276)
        at io.grpc.stub.ClientCalls.asyncStreamingRequestCall(ClientCalls.java:266)
        at io.grpc.stub.ClientCalls.asyncBidiStreamingCall(ClientCalls.java:106)
        at com.google.cloud.speech.v1.SpeechGrpc$SpeechStub.streamingRecognize(SpeechGrpc.java:217)
        at com.google.cloud.android.speech.SpeechService.startRecognizing(SpeechService.java:264)
        at com.google.cloud.android.speech.MainActivity$1.onVoiceStart(MainActivity.java:62)
        at com.google.cloud.android.speech.VoiceRecorder$ProcessVoice.run(VoiceRecorder.java:199)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.IllegalStateException: OAuth2Credentials instance does not support refreshing the access token. An instance with a new access token should be used, or a derived type that supports refreshing.
        at com.google.auth.oauth2.OAuth2Credentials.refreshAccessToken(OAuth2Credentials.java:208)
        at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:175)
        at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:161)
        at com.google.cloud.android.speech.SpeechService$GoogleCredentialsInterceptor.getRequestMetadata(SpeechService.java:532)
        at com.google.cloud.android.speech.SpeechService$GoogleCredentialsInterceptor.access$900(SpeechService.java:450)
        at com.google.cloud.android.speech.SpeechService$GoogleCredentialsInterceptor$1.checkedStart(SpeechService.java:474)
        at io.grpc.ClientInterceptors$CheckedForwardingClientCall.start(ClientInterceptors.java:194)
        at io.grpc.stub.ClientCalls.startCall(ClientCalls.java:276) 
        at io.grpc.stub.ClientCalls.asyncStreamingRequestCall(ClientCalls.java:266) 
        at io.grpc.stub.ClientCalls.asyncBidiStreamingCall(ClientCalls.java:106) 
        at com.google.cloud.speech.v1.SpeechGrpc$SpeechStub.streamingRecognize(SpeechGrpc.java:217) 
        at com.google.cloud.android.speech.SpeechService.startRecognizing(SpeechService.java:264) 
        at com.google.cloud.android.speech.MainActivity$1.onVoiceStart(MainActivity.java:62) 
        at com.google.cloud.android.speech.VoiceRecorder$ProcessVoice.run(VoiceRecorder.java:199) 
        at java.lang.Thread.run(Thread.java:764) 

I could use the sample code:

 final InputStream stream = getResources().openRawResource(R.raw.credential);
 final GoogleCredentials credentials = 
 GoogleCredentials.fromStream(stream).createScoped(SCOPE);
 final AccessToken token = credentials.refreshAccessToken();

and get a working Token, and get its token value by using getTokenValue() and get it expiration time by getExpirationTime. Then create a Token instance: AccessToken token = new AccessToken(tokenValue, expiresIn); to return a AccessToken instance.

It will cause the same error. So my question is that how to create an working AccessToken instance when I get the token value string and the expiration time.

Thanks in advance.