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 = ...;/*** 获取授权服务的 url 地址*/URL url = null; // 后台授权服务的 url 地址try {url = new URL("your_auth_server_url");} catch (MalformedURLException e) {e.printStackTrace();}/*** 初始化 {@link QCloudCredentialProvider} 对象,来给 SDK 提供临时密钥。*/QCloudCredentialProvider credentialProvider = new SessionCredentialProvider(new HttpRequest.Builder<String>().url(url).method("GET").build());CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig, credentialProvider);
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 {// 首先从您的临时密钥服务器获取包含了签名信息的响应....// 然后解析响应,获取密钥信息String tmpSecretId = ...;String tmpSecretKey = ...;String sessionToken = ...;long expiredTime = ...;// 返回服务器时间作为签名的起始时间long beginTime = ...;// todo something you want// 最后返回临时密钥信息对象return new SessionQCloudCredentials(tmpSecretId, tmpSecretKey, sessionToken, beginTime, expiredTime);}}
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 = ...;/*** 初始化 {@link QCloudCredentialProvider} 对象,来给 SDK 提供临时密钥。*/QCloudCredentialProvider credentialProvider = new MyCredentialProvider();CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig, credentialProvider);
//AppDelegate.m//AppDelegate需遵循QCloudCredentailFenceQueueDelegate协议//- (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”;//请求的URL[request setConfigureBlock:^(QCloudRequestSerializer *requestSerializer, QCloudResponseSerializer *responseSerializer) {requestSerializer.serializerBlocks = @[QCloudURLFuseWithURLEncodeParamters];responseSerializer.serializerBlocks = @[QCloudAcceptRespnseCodeBlock([NSSet setWithObjects:@(200), nil],nil),//规定返回码是200以外的时候返回错误QCloudResponseJSONSerilizerBlock];//按照JSON格式解析返回的数据}];[request setFinishBlock:^(id response, NSError *error) {if (error) {error = [NSError errorWithDomain:@"com.tac.test" code:-1111 userInfo:@{NSLocalizedDescriptionKey:@"没有获取到临时密钥"}];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:@"返回的服务器时间"]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];}
pod install
本页内容是否解决了您的问题?