
import android.content.Context;import com.tencent.cos.xml.*;import com.tencent.qcloud.core.auth.*;import com.tencent.qcloud.core.common.*;import com.tencent.qcloud.core.http.*;import java.net.*;Context context = ...;CosXmlServiceConfig cosXmlServiceConfig = ...;/*** Get the authorization service URL*/URL url = null; // URL of the backend authorization servicetry{url = new URL("your_auth_server_url");} catch (MalformedURLException e) {e.printStackTrace();}/*** Initialize the {@link QCloudCredentialProvider} object to provide a temporary key to the SDK.*/QCloudCredentialProvider credentialProvider = new SessionCredentialProvider(new HttpRequest.Builder<String>().url(url).method("GET").build());CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig, credentialProvider);
BasicLifecycleCredentialProvider class and implement its fetchNewCredentials() for custom configurations. For example, you can customize the HTTP response body for the temporary key service to return the server time to the device as the signature start time so as to avoid signature error caused by big device time difference. You can also choose to use other protocols for the communication between the end device and the service side.MyCredentialProvider class first:import android.content.Context;import com.tencent.cos.xml.*;import com.tencent.qcloud.core.auth.*;import com.tencent.qcloud.core.common.*;import com.tencent.qcloud.core.http.*;import java.net.*;public class MyCredentialProvider extends BasicLifecycleCredentialProvider {@Overrideprotected QCloudLifecycleCredentials fetchNewCredentials() throws QCloudClientException {// First, get the response containing a signature from your temporary key server.....// Then, parse the response to get the key.String tmpSecretId = ...;String tmpSecretKey = ...;String sessionToken = ...;long expiredTime = ...;// Return server time as the start time of the signature.long beginTime = ...;// todo something you want// Finally return the temporary key info.return new SessionQCloudCredentials(tmpSecretId, tmpSecretKey, sessionToken, beginTime, expiredTime);}}
MyCredentialProvider instance:import android.content.Context;import com.tencent.cos.xml.*;import com.tencent.qcloud.core.auth.*;import com.tencent.qcloud.core.common.*;import com.tencent.qcloud.core.http.*;import java.net.*;Context context = ...;CosXmlServiceConfig cosXmlServiceConfig = ...;/*** Initialize the {@link QCloudCredentialProvider} to provide a temporary key to the SDK.*/QCloudCredentialProvider credentialProvider = new MyCredentialProvider();CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig, credentialProvider);
//AppDelegate.m//AppDelegate must follow QCloudCredentailFenceQueueDelegate protocol//- (BOOL)application:(UIApplication * )application didFinishLaunchingWithOptions:(NSDictionary * )launchOptions {// init stepself.credentialFenceQueue = [QCloudCredentailFenceQueue new];self.credentialFenceQueue.delegate = self;return YES;}
- (void) fenceQueue:(QCloudCredentailFenceQueue * )queue requestCreatorWithContinue:(QCloudCredentailFenceQueueContinue)continueBlock
- (void)fenceQueue:(QCloudCredentailFenceQueue *)queue requestCreatorWithContinue:(QCloudCredentailFenceQueueContinue)continueBlock {QCloudHTTPRequest* request = [QCloudHTTPRequest new];request.requestData.serverURL = @“your sign service url”;//Request URL[request setConfigureBlock:^(QCloudRequestSerializer *requestSerializer, QCloudResponseSerializer *responseSerializer) {requestSerializer.serializerBlocks = @[QCloudURLFuseWithURLEncodeParamters];responseSerializer.serializerBlocks = @[QCloudAcceptRespnseCodeBlock([NSSet setWithObjects:@(200), nil],nil),//An error is returned when the return code is other than 200.QCloudResponseJSONSerilizerBlock];//Return element parsed in JSON format}];[request setFinishBlock:^(id response, NSError *error) {if (error) {error = [NSError errorWithDomain:@"com.tac.test" code:-1111 userInfo:@{NSLocalizedDescriptionKey:@"No temporary key is obtained."}];continueBlock(nil, error);} else {QCloudCredential* crendential = [[QCloudCredential alloc] init];crendential.secretID = response[@"data"][@"credentials"][@"tmpSecretId"];crendential.secretKey = response[@"data"][@"credentials"][@"tmpSecretKey"];credential.startDate =[NSDate dateWithTimeIntervalSince1970:@"Returned server time"]crendential.experationDate = [NSDate dateWithTimeIntervalSinceNow:[response[@"data"][@"expiredTime"] intValue]];crendential.token = response[@"data"][@"credentials"][@"sessionToken"];;QCloudAuthentationV5Creator* creator = [[QCloudAuthentationV5Creator alloc] initWithCredential:crendential];continueBlock(creator, nil);}}];[[QCloudHTTPSessionManager shareClient] performRequest:request];}
QCloudCOSXMLDemo/QCloudCOSXMLDemo/key.json by adding your APPID, secretID, and secretKey, and run this command:pod install
masukan